| 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" |
| 26 | #include "fpl_EsriHuffman.h" |
| 27 | #include <assert.h> |
| 28 | #include <cmath> |
| 29 | #include <cstring> |
| 30 | #include <climits> |
| 31 | |
| 32 | USING_NAMESPACE_LERC |
| 33 | |
| 34 | static const bool use_esri_huffman = true; |
| 35 | static const bool use_rle = true; |
| 36 | |
| 37 | size_t fpl_Compression::extract_buffer(const char* data, size_t size, size_t uncompressed_size, char** output) |
| 38 | { |
| 39 | if (use_esri_huffman) |
| 40 | { |
| 41 | size_t expected_size = uncompressed_size; |
| 42 | |
| 43 | bool ret = fpl_EsriHuffman::DecodeHuffman((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 | assert(ret); |
| 49 | return uncompressed_size; |
| 50 | } |
| 51 | |
| 52 | assert(0); |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | size_t fpl_Compression::compress_buffer(const char* data, size_t size, char** output, bool fast) |
| 57 | { |
| 58 | if (use_esri_huffman) |
| 59 | { |
| 60 | if (fast && !output) |
| 61 | { |
| 62 | // return getCompressedSize (data, size); |
| 63 | |
| 64 | return getEntropySize((unsigned char*)data, size); // this is approximate but fastest |
| 65 | } |
| 66 | |
| 67 | assert(size > 0); |
| 68 | |
| 69 | int ret = fpl_EsriHuffman::EncodeHuffman(data, size, (unsigned char**)output, 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_MAX + 1; |
| 77 | } |
| 78 | |
| 79 | return ret; |
| 80 | } |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | long fpl_Compression::getEntropySize(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_MULT) |
| 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 | } |