| 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 | #include "fpl_Compression.h"cxx . . $ `<file>/src/LercLib/fpl_Compression.h`/ |
| 26 | #include "fpl_EsriHuffman.h"cxx . . $ `<file>/src/LercLib/fpl_EsriHuffman.h`/ |
| 27 | #include <assert.h> |
| 28 | #include <cmath> |
| 29 | #include <cstring> |
| 30 | #include <climits> |
| 31 | |
| 32 | USING_NAMESPACE_LERCcxx . . $ `src/LercLib/Defines.h:37:9`! |
| 33 | |
| 34 | static const bool use_esri_huffmancxx . . $ use_esri_huffman. = true; |
| 35 | static const bool use_rlecxx . . $ use_rle. = true; |
| 36 | |
| 37 | size_t fpl_Compressioncxx . . $ LercNS/fpl_Compression#::extract_buffercxx . . $ LercNS/fpl_Compression#extract_buffer(a437f2c8d8e82d1f).(const char* data, size_t size, size_t uncompressed_size, char** output) |
| 38 | { |
| 39 | if (use_esri_huffmancxx . . $ use_esri_huffman.) |
| 40 | { |
| 41 | size_t expected_size = uncompressed_size; |
| 42 | |
| 43 | bool ret = fpl_EsriHuffmancxx . . $ LercNS/fpl_EsriHuffman#::DecodeHuffmancxx . . $ LercNS/fpl_EsriHuffman#DecodeHuffman(bd1c51dd505c7d63).((const unsigned char*)data, size, expected_size, (unsigned char**)output); |
| 44 | |
| 45 | //if (!ret) |
| 46 | // printf("input size %zu, expected out %zu\n", size, uncompressed_size); |
| 47 | |
| 48 | assertcxx . . $ `<external>/4398592474888995393/assert.h:137:11`!(ret); |
| 49 | return uncompressed_size; |
| 50 | } |
| 51 | |
| 52 | assertcxx . . $ `<external>/4398592474888995393/assert.h:137:11`!(0); |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | size_t fpl_Compressioncxx . . $ LercNS/fpl_Compression#::compress_buffercxx . . $ LercNS/fpl_Compression#compress_buffer(71aa3d091b4f84ee).(const char* data, size_t size, char** output, bool fast) |
| 57 | { |
| 58 | if (use_esri_huffmancxx . . $ use_esri_huffman.) |
| 59 | { |
| 60 | if (fast && !output) |
| 61 | { |
| 62 | // return getCompressedSize (data, size); |
| 63 | |
| 64 | return getEntropySizecxx . . $ LercNS/fpl_Compression#getEntropySize(e0e0ee6b764d745e).((unsigned char*)data, size); // this is approximate but fastest |
| 65 | } |
| 66 | |
| 67 | assertcxx . . $ `<external>/4398592474888995393/assert.h:137:11`!(size > 0); |
| 68 | |
| 69 | int ret = fpl_EsriHuffmancxx . . $ LercNS/fpl_EsriHuffman#::EncodeHuffmancxx . . $ LercNS/fpl_EsriHuffman#EncodeHuffman(1bb1bf4c4e784b76).(data, size, (unsigned char**)output, use_rlecxx . . $ use_rle.); |
| 70 | |
| 71 | if (ret < 0) |
| 72 | { |
| 73 | #ifdef _DEBUG |
| 74 | // fprintf(stderr, "Huffman failed. Input size: %zd. Code: %d\n", size, ret); |
| 75 | #endif |
| 76 | return (size_t)UINT_MAXcxx . . $ `<external>/2419331716647297546/limits.h:124:9`! + 1; |
| 77 | } |
| 78 | |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | long fpl_Compressioncxx . . $ LercNS/fpl_Compression#::getEntropySizecxx . . $ LercNS/fpl_Compression#getEntropySize(e0e0ee6b764d745e).(const unsigned char* ptr, const size_t size) |
| 86 | { |
| 87 | unsigned long table[256]; |
| 88 | |
| 89 | memset(table, 0, sizeof(table)); |
| 90 | |
| 91 | int total_count = 0; |
| 92 | |
| 93 | for (size_t i = 0; i < size; i += PRIME_MULTcxx . . $ `src/LercLib/fpl_Compression.h:33:9`!) |
| 94 | { |
| 95 | table[ptr[i]]++; |
| 96 | total_count++; |
| 97 | } |
| 98 | |
| 99 | double total_bits = 0; |
| 100 | |
| 101 | for (size_t i = 0; i < 256; i++) |
| 102 | { |
| 103 | if (table[i] == 0) continue; |
| 104 | |
| 105 | double p = (double)total_count / table[i]; |
| 106 | |
| 107 | double bits = log2(p); |
| 108 | |
| 109 | total_bits += (bits * table[i]); |
| 110 | } |
| 111 | |
| 112 | return (long)((total_bits + 7) / 8); |
| 113 | } |