Debian Codegraph @

src/LercLib/fpl_Lerc2Ext.cpp

raw symbols SCIP
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_Lerc2Ext.h"cxx . . $ `<file>/src/LercLib/fpl_Lerc2Ext.h`/
26 #include "fpl_Compression.h"cxx . . $ `<file>/src/LercLib/fpl_Compression.h`/
27 #include <assert.h>
28 #include <cmath>
29 #include <cstring>
30 #include <stdint.h>
31 #include <algorithm>
32
33 USING_NAMESPACE_LERCcxx . . $ `src/LercLib/Defines.h:37:9`!
34
35 void lerc_assertcxx . . $ lerc_assert(b45ddcbc8423892c).(bool v)
36 {
37 if (v == false)
38 throw "Assertion failed";
39 }
40
41 template<typename T>
42 size_t getMinIndexcxx . . $ getMinIndex(e07aaf67656fc9fe).(const T* array, size_t size)
43 {
44 T min_value = array[0];
45 size_t ret = 0;
46
47 for (size_t i = 1; i < size; i++)
48 {
49 if (array[i] < min_value)
50 {
51 min_value = array[i];
52 ret = i;
53 }
54 }
55
56 return ret;
57 }
58
59 struct TestBlockcxx . . $ TestBlock#
60 {
61 long topcxx . . $ TestBlock#top., heightcxx . . $ TestBlock#height.;
62 };
63
64 static void generateListOfTestBlockscxx . . $ generateListOfTestBlocks(84f9e277a9315f4).(const int width, const int height, stdcxx . . $ std/::vectorcxx . . $ std/vector# <TestBlockcxx . . $ TestBlock#>& blocks)
65 {
66 size_t size = (size_t)width * height;
67
68 lerc_assertcxx . . $ lerc_assert(b45ddcbc8423892c).(size > 0);
69
70 const int block_target_size = 8 * 1024;
71
72 double t = (round)((double)size / block_target_size);
73
74 int count = (int)round(sqrt(t + 1)); // Always at least 1. We want count to grow but not linearly as size grows.
75 // A more conservative scheme would be to use log not sqrt.
76
77 // when block size is too wide (>100K) we may need to consider taking a "narrower part"
78
79 int block_height = block_target_size / width;
80
81 if (block_height < 4) block_height = 4;
82
83 while ((count * block_height > height) && (count > 1)) count--;
84
85 float top_margin = (float)((height - count * block_height) / (2.0 * count));
86
87 float delta = 2.0f * top_margin + block_height;
88
89 for (int i = 0; i < count; i++)
90 {
91 TestBlockcxx . . $ TestBlock# tb;
92
93 tb.topcxx . . $ TestBlock#top. = (long)(top_margin + delta * i);
94 tb.heightcxx . . $ TestBlock#height. = block_height;
95 if (tb.topcxx . . $ TestBlock#top. < 0) tb.topcxx . . $ TestBlock#top. = 0;
96 if (tb.topcxx . . $ TestBlock#top. + tb.heightcxx . . $ TestBlock#height. > height) tb.heightcxx . . $ TestBlock#height. = height - tb.topcxx . . $ TestBlock#top.;
97
98 if (tb.heightcxx . . $ TestBlock#height. > 0)
99 blocks.push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(tb);
100 }
101 }
102
103 static void setDerivativePrimecxx . . $ setDerivativePrime(6165b45f45ef689c).(unsigned char* data, size_t size)
104 {
105 int off = (int)size - 1;
106
107 off = PRIME_MULTcxx . . $ `src/LercLib/fpl_Compression.h:33:9`! * (off / PRIME_MULTcxx . . $ `src/LercLib/fpl_Compression.h:33:9`!);
108
109 unsigned char* ptr = data + off;
110 //for (int i = (int)off; i >= 1; i -= PRIME_MULT)
111 while (ptr >= data + 1)
112 {
113 *ptr -= *(ptr - 1);
114 ptr -= PRIME_MULTcxx . . $ `src/LercLib/fpl_Compression.h:33:9`!;
115 }
116 }
117
118 static void setDerivativecxx . . $ setDerivative(c7957102a30d7ee2).(unsigned char* data, size_t size, const int level)
119 {
120 if (level == 0) return;
121
122 for (int l = 1; l <= level; l++)
123 {
124 unsigned char* ptr = data + size - 1;
125 for (int i = (int)size - 1; i >= l; i--)
126 {
127 *ptr -= *(ptr - 1);
128 ptr--;
129 }
130 }
131 }
132
133 unsigned char* restoreSequencecxx . . $ restoreSequence(21b352d61946858b).(unsigned char* data, size_t size, const int level, bool make_copy)
134 {
135 assertcxx . . $ `<external>/4398592474888995393/assert.h:137:11`!(size > 0);
136
137 unsigned char* copy = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:416:9`!;
138
139 if (make_copy)
140 {
141 copy = (unsigned char*)malloc(size);
142
143 if (!copy)
144 {
145 //perror("Out of memory.");
146 return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:416:9`!;
147 }
148
149 memcpy(copy, data, size);
150 }
151 else
152 copy = data;
153
154 if (level == 0)
155 return copy;
156
157 for (int l = level; l > 0; l--)
158 {
159 unsigned char* ptr = copy + l;
160
161 for (int i = l; i < (int)size; i++)
162 {
163 *ptr += *(ptr - 1);
164 ptr++;
165 }
166 }
167
168 return copy;
169 }
170
171 static size_t testBlocksSizecxx . . $ testBlocksSize(d5c22d137bf19696).(stdcxx . . $ std/::vectorcxx . . $ std/vector# <TestBlockcxx . . $ TestBlock#>& blocks, const UnitTypecxx . . $ LercNS/UnitType# unit_type, const void* _data, const long raster_width, bool test_first_byte_delta)
172 {
173 size_t ret = 0;
174
175 const uint8_tcxx . . $ uint8_t#* data = (const uint8_tcxx . . $ uint8_t#*)_data;
176
177 const size_t unit_size = UnitTypescxx . . $ LercNS/UnitTypes#::sizecxx . . $ LercNS/UnitTypes#size(5871b0c0ce9b5dff).(unit_type);
178
179 for (size_t blk = 0; blk < blocks.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); blk++)
180 {
181 TestBlockcxx . . $ TestBlock#& tb = blocks[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).blk];
182
183 size_t start = unit_size * tb.topcxx . . $ TestBlock#top. * raster_width;
184 size_t length = tb.heightcxx . . $ TestBlock#height. * raster_width;
185
186 // input_len += length * unit_size; //sizeof(uint32_t);
187
188 char* plane_buffer = (char*)malloc(length);
189
190 assertcxx . . $ `<external>/4398592474888995393/assert.h:137:11`!(plane_buffer);
191
192 for (int byte = 0; byte < (int)unit_size; byte++)
193 {
194 char* ptr = (char*)(data + start);
195
196 ptr += byte;
197
198 for (size_t i = 0; i < length; i++)
199 {
200 plane_buffer[i] = *ptr;
201
202 ptr += unit_size;
203 }
204
205 size_t plane_encoded = fpl_Compressioncxx . . $ LercNS/fpl_Compression#::compress_buffercxx . . $ LercNS/fpl_Compression#compress_buffer(71aa3d091b4f84ee).((const char*)plane_buffer, length, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:416:9`!, true);
206
207 // test with delta 1:
208
209 if (test_first_byte_delta)
210 {
211 // setDerivative ((unsigned char *)plane_buffer, length, 1);
212
213 setDerivativePrimecxx . . $ setDerivativePrime(6165b45f45ef689c).((unsigned char*)plane_buffer, length);
214
215 size_t plane_encoded2 = fpl_Compressioncxx . . $ LercNS/fpl_Compression#::compress_buffercxx . . $ LercNS/fpl_Compression#compress_buffer(71aa3d091b4f84ee).((const char*)plane_buffer, length, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:416:9`!, true);
216
217 ret += (stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a).)(plane_encoded, plane_encoded2);
218 }
219 else
220 {
221 ret += plane_encoded;
222 }
223 }
224
225 free(plane_buffer);
226 }
227
228 return ret;
229 }
230
231
232 /////////////////////////////////////////////////////////////////////////////////////////////////////////
233
234
235 int getBestLevel2cxx . . $ getBestLevel2(2aa6cce9a8def02e).(const unsigned char* ptr, size_t size, const int max_delta_order)
236 {
237 stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::paircxx . . $ std/pair#<size_t, int> > snippetscxx . . $ std/vector#vector(49f6e7a06ebc5aa8).;
238
239 const unsigned int targetSampleSize = 1024 * 8;
240
241 double t = round((double)size / targetSampleSize);
242
243 int count = (int)round(sqrt(t + 1)); // Always at least 1. We want count to grow but not linearly as size grows.
244 // A more conservative scheme would be to use log not sqrt.
245
246 while (count * targetSampleSize > size && (count > 0)) count--;
247
248 float top_margin = (float)(((int)size - count * targetSampleSize) / (2.0 * count));
249
250 float delta = 2.0f * top_margin + targetSampleSize;
251
252 for (int i = 0; i < count; i++)
253 {
254 long start = (long)(top_margin + delta * i);
255 int len = targetSampleSize;
256
257 if (start < 0)
258 start = 0;
259 if (start + len > (int)size)
260 len = (int)size - start;
261
262 assertcxx . . $ `<external>/4398592474888995393/assert.h:137:11`!(len > 0);
263
264 if (len > 0)
265 snippets.push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(stdcxx . . $ std/pair#pair(f166319c93b32899).::make_paircxx . . $ std/make_pair(1d30b6e115203116).(start, len));
266 }
267
268 unsigned char* copy = (unsigned char*)malloc(size);
269
270 if (!copy)
271 return 0; // return 0 as fall-back "default" delta.
272
273 memcpy(copy, ptr, size);
274
275 size_t best_comp = 0;
276
277 int ret = 0;
278
279 for (int l = 0; l <= max_delta_order; l++)
280 {
281 if (l > 0)
282 {
283 for (size_t s = 0; s < snippets.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); s++)
284 {
285 size_t start = snippets[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).s].firstcxx . . $ std/pair#first.;
286 int len = snippets[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).s].secondcxx . . $ std/pair#second.;
287
288 for (int i = (int)start + len - 1; i >= (int)start + l; i--)
289 {
290 copy[i] -= copy[i - 1];
291 }
292 }
293 }
294
295 size_t comp = 0;
296
297 for (size_t i = 0; i < snippets.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); i++)
298 {
299 size_t start = snippets[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i].firstcxx . . $ std/pair#first.;
300 int len = snippets[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i].secondcxx . . $ std/pair#second.;
301
302 comp += fpl_Compressioncxx . . $ LercNS/fpl_Compression#::compress_buffercxx . . $ LercNS/fpl_Compression#compress_buffer(71aa3d091b4f84ee).((const char*)copy + start, len, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:416:9`!, true);
303 }
304
305 if (comp < best_comp || l == 0)
306 {
307 best_comp = comp;
308 ret = l;
309 }
310 else
311 {
312 break; // if deteriorating, stop.
313 }
314 }
315
316 free(copy);
317
318 return ret;
319 }
320
321 ///////////////////////////////////////////////////////////////////////////////////////////////
322
323 int getBestLevelcxx . . $ getBestLevel(2aa6cce9a8def02e).(const unsigned char* ptr, size_t size, const int max_delta_order)
324 {
325 if (max_delta_order == 0)
326 return 0;
327
328 return getBestLevel2cxx . . $ getBestLevel2(2aa6cce9a8def02e).(ptr, size, max_delta_order);
329 }
330
331 ///////////////////////////////////////////////////////////////////////////////////////////////
332
333 LosslessFPCompressioncxx . . $ LercNS/LosslessFPCompression#::~cxx . . $ LercNS/LosslessFPCompression#`~LosslessFPCompression`(ced63f7c635d850d).LosslessFPCompressioncxx . . $ LercNS/LosslessFPCompression#()
334 {
335 delete m_data_slicecxx . . $ LercNS/LosslessFPCompression#m_data_slice.;
336 }
337
338 void LosslessFPCompressioncxx . . $ LercNS/LosslessFPCompression#::selectInitialLinearOrCrossDeltacxx . . $ LercNS/LosslessFPCompression#selectInitialLinearOrCrossDelta(b67911cd92a65e33).(const UnitTypecxx . . $ LercNS/UnitType# unit_type, void* pData, const int iWidth,
339 const int iHeight, int& initial_delta, bool& use_cross, bool test_first_byte_delta, size_t* stats)
340 {
341 stdcxx . . $ std/::vectorcxx . . $ std/vector# <TestBlockcxx . . $ TestBlock#> blockscxx . . $ std/vector#vector(49f6e7a06ebc5aa8).;
342
343 size_t local_stats[3] = { 0 };
344
345 if (stats == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:416:9`!)
346 {
347 stats = local_stats;
348 }
349
350 generateListOfTestBlockscxx . . $ generateListOfTestBlocks(84f9e277a9315f4).(iWidth, iHeight, blocks);
351
352 size_t est = 0;
353
354 // unchanged block:
355 est = testBlocksSizecxx . . $ testBlocksSize(d5c22d137bf19696).(blocks, unit_type, pData, iWidth, test_first_byte_delta);
356
357 if (stats) { stats[0] += est; }
358
359 // linear part:
360
361 UnitTypescxx . . $ LercNS/UnitTypes#::setBlockDerivativecxx . . $ LercNS/UnitTypes#setBlockDerivative(69239a207801649b).(unit_type, pData, iWidth, iHeight, 1, 1);
362
363 est = testBlocksSizecxx . . $ testBlocksSize(d5c22d137bf19696).(blocks, unit_type, pData, iWidth, test_first_byte_delta);
364
365 if (stats) { stats[1] += est; }
366
367 UnitTypescxx . . $ LercNS/UnitTypes#::setCrossDerivativecxx . . $ LercNS/UnitTypes#setCrossDerivative(69239a207801649b).(unit_type, pData, iWidth, iHeight, 2, 2);
368
369 est = testBlocksSizecxx . . $ testBlocksSize(d5c22d137bf19696).(blocks, unit_type, pData, iWidth, test_first_byte_delta);
370
371 if (stats) { stats[2] += est; }
372
373 size_t min_index = getMinIndexcxx . . $ getMinIndex(e07aaf67656fc9fe).<size_t>(stats, 3);
374
375 if (min_index == 2)
376 {
377 use_cross = true;
378 initial_delta = 2;
379 }
380 else if (min_index == 1)
381 {
382 use_cross = false;
383 initial_delta = 1;
384 }
385 else
386 {
387 use_cross = false;
388 initial_delta = 0;
389 }
390 }
391
392 int LosslessFPCompressioncxx . . $ LercNS/LosslessFPCompression#::compressedLengthcxx . . $ LercNS/LosslessFPCompression#compressedLength(455f465bc33b4cdf).() const
393 {
394 int ret = 1; // predictor code
395
396 for (auto b : m_data_slicecxx . . $ LercNS/LosslessFPCompression#m_data_slice.->m_bufferscxx . . $ LercNS/LosslessFPCompression#compressedDataSlice#m_buffers.)
397 {
398 ret += b->compressed_sizecxx . . $ LercNS/LosslessFPCompression#outBlockBuffer#compressed_size.;
399 ret += 6; // 2 bytes plus compr. size. We can use 1 byte in place of 2 by packing level and byte index.
400 }
401
402 return ret;
403 }
404
405 bool LosslessFPCompressioncxx . . $ LercNS/LosslessFPCompression#::EncodeHuffmanFltcxx . . $ LercNS/LosslessFPCompression#EncodeHuffmanFlt(3a3fc37189ba3738).(unsigned char ** ppByte)
406 {
407 memcpy(*ppByte, &(m_data_slicecxx . . $ LercNS/LosslessFPCompression#m_data_slice.->m_predictor_codecxx . . $ LercNS/LosslessFPCompression#compressedDataSlice#m_predictor_code.), sizeof(m_data_slicecxx . . $ LercNS/LosslessFPCompression#m_data_slice.->m_predictor_codecxx . . $ LercNS/LosslessFPCompression#compressedDataSlice#m_predictor_code.));
408 *ppByte += sizeof(m_data_slicecxx . . $ LercNS/LosslessFPCompression#m_data_slice.->m_predictor_codecxx . . $ LercNS/LosslessFPCompression#compressedDataSlice#m_predictor_code.);
409
410 for (auto b : m_data_slicecxx . . $ LercNS/LosslessFPCompression#m_data_slice.->m_bufferscxx . . $ LercNS/LosslessFPCompression#compressedDataSlice#m_buffers.)
411 {
412 memcpy(*ppByte, &(b->byte_indexcxx . . $ LercNS/LosslessFPCompression#outBlockBuffer#byte_index.), sizeof(b->byte_indexcxx . . $ LercNS/LosslessFPCompression#outBlockBuffer#byte_index.));
413 *ppByte += sizeof(b->byte_indexcxx . . $ LercNS/LosslessFPCompression#outBlockBuffer#byte_index.);
414 memcpy(*ppByte, &(b->best_levelcxx . . $ LercNS/LosslessFPCompression#outBlockBuffer#best_level.), sizeof(b->best_levelcxx . . $ LercNS/LosslessFPCompression#outBlockBuffer#best_level.));
415 *ppByte += sizeof(b->best_levelcxx . . $ LercNS/LosslessFPCompression#outBlockBuffer#best_level.);
416 memcpy(*ppByte, &(b->compressed_sizecxx . . $ LercNS/LosslessFPCompression#outBlockBuffer#compressed_size.), sizeof(b->compressed_sizecxx . . $ LercNS/LosslessFPCompression#outBlockBuffer#compressed_size.));
417 *ppByte += sizeof(b->compressed_sizecxx . . $ LercNS/LosslessFPCompression#outBlockBuffer#compressed_size.);
418 memcpy(*ppByte, b->compressedcxx . . $ LercNS/LosslessFPCompression#outBlockBuffer#compressed., b->compressed_sizecxx . . $ LercNS/LosslessFPCompression#outBlockBuffer#compressed_size.);
419 *ppByte += b->compressed_sizecxx . . $ LercNS/LosslessFPCompression#outBlockBuffer#compressed_size.;
420 }
421
422 for (auto v : m_data_slicecxx . . $ LercNS/LosslessFPCompression#m_data_slice.->m_bufferscxx . . $ LercNS/LosslessFPCompression#compressedDataSlice#m_buffers.)
423 {
424 delete v;
425 }
426
427 m_data_slicecxx . . $ LercNS/LosslessFPCompression#m_data_slice.->m_bufferscxx . . $ LercNS/LosslessFPCompression#compressedDataSlice#m_buffers..clearcxx . . $ std/vector#clear(ced63f7c635d850d).();
428
429 return true;
430 }
431
432 bool LosslessFPCompressioncxx . . $ LercNS/LosslessFPCompression#::ComputeHuffmanCodesFltcxx . . $ LercNS/LosslessFPCompression#ComputeHuffmanCodesFlt(dbf79bfbd080cdb0).(const void* input, bool bIsDouble,
433 int iCols, int iRows, int iDepth)
434 {
435 if (iDepth == 1)
436 {
437 if (m_data_slicecxx . . $ LercNS/LosslessFPCompression#m_data_slice. && !m_data_slicecxx . . $ LercNS/LosslessFPCompression#m_data_slice.->m_bufferscxx . . $ LercNS/LosslessFPCompression#compressedDataSlice#m_buffers..emptycxx . . $ std/vector#empty(3482b152b9333168).())
438 {
439 // we decided not to write compressed output last time.
440 // in this case, old compressed content must be removed.
441 for (auto v : m_data_slicecxx . . $ LercNS/LosslessFPCompression#m_data_slice.->m_bufferscxx . . $ LercNS/LosslessFPCompression#compressedDataSlice#m_buffers.)
442 {
443 delete v;
444 }
445
446 m_data_slicecxx . . $ LercNS/LosslessFPCompression#m_data_slice.->m_bufferscxx . . $ LercNS/LosslessFPCompression#compressedDataSlice#m_buffers..clearcxx . . $ std/vector#clear(ced63f7c635d850d).();
447 }
448 return ComputeHuffmanCodesFltSlicecxx . . $ LercNS/LosslessFPCompression#ComputeHuffmanCodesFltSlice(748b46cc5a22b819). (input, bIsDouble, iCols, iRows);
449 }
450 else
451 {
452 return ComputeHuffmanCodesFltSlicecxx . . $ LercNS/LosslessFPCompression#ComputeHuffmanCodesFltSlice(748b46cc5a22b819).(input, bIsDouble, iDepth, iCols * iRows);
453 }
454 }
455
456 bool LosslessFPCompressioncxx . . $ LercNS/LosslessFPCompression#::ComputeHuffmanCodesFltSlicecxx . . $ LercNS/LosslessFPCompression#ComputeHuffmanCodesFltSlice(748b46cc5a22b819). (const void* pInput, bool bIsDouble, int iCols, int iRows)
457 {
458 // 1. copy input; when input is 32-bit floats, move bits of copied input values.
459 // 2. copy to temp buffer.
460 // 3. perform selectInitialLinearOrCrossDelta on temp copy; deallocate temp copy.
461 // 4. apply best predictor.
462 // 5. compress individual byte planes while applying additional dynamic delta (bestLevel).
463 // 6. save compessed byte planes in m_buffers and predictor in m_predictor_code.
464 // 7. deallocate copied input.
465
466 UnitTypecxx . . $ LercNS/UnitType# unit_type = bIsDouble ? UNIT_TYPE_DOUBLEcxx . . $ LercNS/UNIT_TYPE_DOUBLE. : UNIT_TYPE_FLOATcxx . . $ LercNS/UNIT_TYPE_FLOAT.;
467
468 int max_byte_delta = MAX_DELTAcxx . . $ `src/LercLib/fpl_Predictor.h:32:9`!; // in pactice it will rarely be more than 3.
469 // I observed values greater than 3 only with double data.
470 // see getMaxByteDelta() call below.
471
472 size_t size = (size_t)iCols * iRows;
473
474 size_t unit_size = UnitTypescxx . . $ LercNS/UnitTypes#::sizecxx . . $ LercNS/UnitTypes#size(5871b0c0ce9b5dff).(unit_type);
475 size_t block_size = size;
476
477 uint8_tcxx . . $ uint8_t#* block_values = (uint8_tcxx . . $ uint8_t#*)malloc(block_size * unit_size);
478
479 memcpy(block_values, pInput, block_size * unit_size);
480
481 if (unit_type == UNIT_TYPE_FLOATcxx . . $ LercNS/UNIT_TYPE_FLOAT.)
482 UnitTypescxx . . $ LercNS/UnitTypes#::doFloatTransformcxx . . $ LercNS/UnitTypes#doFloatTransform(2769ff323d6644d4).((uint32_tcxx . . $ uint32_t#*)block_values, size);
483
484 size_t stats1[3] = { 0 };
485
486 int block_width = iCols, block_height = iRows;
487
488 int dummy_delta = 0;
489 bool dummy_cross = false;
490 bool test_first_byte_delta = true;
491
492 uint8_tcxx . . $ uint8_t#* copy = (uint8_tcxx . . $ uint8_t#*)malloc(block_size * unit_size);
493
494 if (!copy)
495 {
496 //perror("Out of memory.");
497 free(block_values);
498 return false;
499 }
500
501 memcpy(copy, block_values, unit_size * block_size);
502
503 selectInitialLinearOrCrossDeltacxx . . $ LercNS/LosslessFPCompression#selectInitialLinearOrCrossDelta(b67911cd92a65e33).(unit_type, copy, block_width, block_height, dummy_delta, dummy_cross, test_first_byte_delta, stats1);
504
505 free(copy);
506
507 size_t min_index = getMinIndexcxx . . $ getMinIndex(e07aaf67656fc9fe).<size_t>(stats1, 3);
508
509 PredictorTypecxx . . $ LercNS/PredictorType# predictor = PREDICTOR_NONEcxx . . $ LercNS/PREDICTOR_NONE.;
510
511 if (min_index < 2)
512 {
513 if (min_index == 1) predictor = PREDICTOR_DELTA1cxx . . $ LercNS/PREDICTOR_DELTA1.;
514 }
515 else
516 {
517 if (min_index == 2) predictor = PREDICTOR_ROWS_COLScxx . . $ LercNS/PREDICTOR_ROWS_COLS.;
518 }
519
520 int w = block_width, h = block_height;
521
522 if (predictor == PREDICTOR_ROWS_COLScxx . . $ LercNS/PREDICTOR_ROWS_COLS.)
523 {
524 if (predictor == PREDICTOR_ROWS_COLScxx . . $ LercNS/PREDICTOR_ROWS_COLS.)
525 UnitTypescxx . . $ LercNS/UnitTypes#::setCrossDerivativecxx . . $ LercNS/UnitTypes#setCrossDerivative(69239a207801649b).(unit_type, block_values, w, h, 2);
526 }
527 else
528 {
529 int delta = 0;
530
531 if (predictor == PREDICTOR_DELTA1cxx . . $ LercNS/PREDICTOR_DELTA1.) delta = 1;
532
533 UnitTypescxx . . $ LercNS/UnitTypes#::setBlockDerivativecxx . . $ LercNS/UnitTypes#setBlockDerivative(69239a207801649b).(unit_type, block_values, w, h, delta);
534 }
535
536 int max_delta = Predictorcxx . . $ LercNS/Predictor#::getMaxByteDeltacxx . . $ LercNS/Predictor#getMaxByteDelta(d8ddbb2269178517).(predictor);
537
538 if (max_byte_delta >= 0 && max_byte_delta < max_delta)
539 max_delta = max_byte_delta;
540
541 unsigned char* block_buff = (unsigned char*)malloc(block_size); // size is the same for all byte planes
542
543 if (!block_buff)
544 {
545 //perror("Out of memory.");
546 free(block_values);
547 return false;
548 }
549
550 if (!m_data_slicecxx . . $ LercNS/LosslessFPCompression#m_data_slice.)
551 m_data_slicecxx . . $ LercNS/LosslessFPCompression#m_data_slice. = new compressedDataSlicecxx . . $ LercNS/LosslessFPCompression#compressedDataSlice#();
552
553 for (int byte = 0; byte < (int)unit_size; byte++)
554 {
555 size_t block_index = 0;
556
557 size_t block_shift = 0;
558
559 block_index = byte;
560
561 size_t len = (size_t)h * w;
562
563 while (block_shift < len)
564 {
565 block_buff[block_shift] = block_values[block_index];
566
567 block_index += unit_size;
568
569 block_shift++;
570 }
571
572 int bestLevel = getBestLevelcxx . . $ getBestLevel(2aa6cce9a8def02e).(block_buff, block_size, max_delta);
573
574 setDerivativecxx . . $ setDerivative(c7957102a30d7ee2).(block_buff, block_size, bestLevel);
575
576 char* compressed = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:416:9`!;
577
578 size_t ret = fpl_Compressioncxx . . $ LercNS/fpl_Compression#::compress_buffercxx . . $ LercNS/fpl_Compression#compress_buffer(71aa3d091b4f84ee).((const char*)block_buff, block_size, &compressed);
579
580 if (ret > UINT32_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:124:10`!) // cannot store compressed size in 32 bits. should not happen.
581 {
582 free(block_buff);
583 free(block_values);
584
585 return false;
586 }
587
588 // Down the road, we also can store compression method code (other than default Huffman)
589 // in upper top 6 bits, since max predicor value is 2.
590
591 m_data_slicecxx . . $ LercNS/LosslessFPCompression#m_data_slice.->m_predictor_codecxx . . $ LercNS/LosslessFPCompression#compressedDataSlice#m_predictor_code. = Predictorcxx . . $ LercNS/Predictor#::getCodecxx . . $ LercNS/Predictor#getCode(6d3690282f9f8b57).(predictor);
592
593 {
594 outBlockBuffercxx . . $ LercNS/LosslessFPCompression#outBlockBuffer#* ob = new outBlockBuffercxx . . $ LercNS/LosslessFPCompression#outBlockBuffer#;
595 ob->compressedcxx . . $ LercNS/LosslessFPCompression#outBlockBuffer#compressed. = compressed;
596 ob->compressed_sizecxx . . $ LercNS/LosslessFPCompression#outBlockBuffer#compressed_size. = (uint32_tcxx . . $ uint32_t#)ret;
597 ob->byte_indexcxx . . $ LercNS/LosslessFPCompression#outBlockBuffer#byte_index. = (unsigned char)byte;
598 ob->best_levelcxx . . $ LercNS/LosslessFPCompression#outBlockBuffer#best_level. = (unsigned char)bestLevel;
599
600 m_data_slicecxx . . $ LercNS/LosslessFPCompression#m_data_slice.->m_bufferscxx . . $ LercNS/LosslessFPCompression#compressedDataSlice#m_buffers..push_backcxx . . $ std/vector#push_back(77d9f17553126ecb).(ob);
601 }
602 }
603
604 free(block_buff);
605 free(block_values);
606
607 return true;
608 }
609
610 /////////////////////////////////////////////////////////////////////////////////////////////////
611
612 bool restoreCrossBytescxx . . $ restoreCrossBytes(847899b31443dfbf).(stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::paircxx . . $ std/pair#<int, char*> >& output_buffers, const size_t input_in_bytes,
613 const size_t cols, const size_t rows, const PredictorTypecxx . . $ LercNS/PredictorType# predictor,
614 //FILE* output,
615 const UnitTypecxx . . $ LercNS/UnitType# unit_type, uint8_tcxx . . $ uint8_t#** output_block)
616 {
617 lerc_assertcxx . . $ lerc_assert(b45ddcbc8423892c).(predictor == PREDICTOR_NONEcxx . . $ LercNS/PREDICTOR_NONE. || predictor == PREDICTOR_ROWS_COLScxx . . $ LercNS/PREDICTOR_ROWS_COLS.);
618
619 size_t unit_size = output_buffers.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).();
620
621 lerc_assertcxx . . $ lerc_assert(b45ddcbc8423892c).(unit_size == UnitTypescxx . . $ LercNS/UnitTypes#::sizecxx . . $ LercNS/UnitTypes#size(5871b0c0ce9b5dff).(unit_type));
622
623 const int delta = Predictorcxx . . $ LercNS/Predictor#::getIntDeltacxx . . $ LercNS/Predictor#getIntDelta(d8ddbb2269178517).(predictor);
624
625 size_t block_size = cols * rows;
626
627 assertcxx . . $ `<external>/4398592474888995393/assert.h:137:11`!(input_in_bytes == block_size);
628
629 uint8_tcxx . . $ uint8_t#* data_buffer = (uint8_tcxx . . $ uint8_t#*)malloc(block_size * unit_size);
630
631 if (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:416:9`! == data_buffer)
632 {
633 //perror("Out of memory.");
634 return false;
635 }
636
637 size_t offset = 0;
638
639 for (size_t i = 0; i < block_size; i++)
640 {
641 for (size_t byte = 0; byte < unit_size; byte++)
642 {
643 int idx = output_buffers[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).byte].firstcxx . . $ std/pair#first.;
644 data_buffer[idx + offset] = output_buffers[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).byte].secondcxx . . $ std/pair#second.[i];
645 }
646
647 offset += unit_size;
648 }
649
650 UnitTypescxx . . $ LercNS/UnitTypes#::restoreCrossBytescxx . . $ LercNS/UnitTypes#restoreCrossBytes(ed9c1a8191464942).(delta, data_buffer, cols, rows, unit_type);
651
652 if (unit_type == UNIT_TYPE_FLOATcxx . . $ LercNS/UNIT_TYPE_FLOAT.)
653 {
654 UnitTypescxx . . $ LercNS/UnitTypes#::undoFloatTransformcxx . . $ LercNS/UnitTypes#undoFloatTransform(2769ff323d6644d4).((uint32_tcxx . . $ uint32_t#*)data_buffer, block_size);
655 }
656
657 if (output_block)
658 {
659 *output_block = data_buffer;
660 }
661 else
662 {
663 free(data_buffer); // Coverity warning fix. Currently, execution never gets here.
664 }
665
666 return true;
667 }
668
669 bool restoreByteOrdercxx . . $ restoreByteOrder(1735502d4e909cf3).(stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::paircxx . . $ std/pair#<int, char*> >& output_buffers,
670 const size_t cols, const size_t rows, const PredictorTypecxx . . $ LercNS/PredictorType# predictor, //FILE* output,
671 const UnitTypecxx . . $ LercNS/UnitType# unit_type, uint8_tcxx . . $ uint8_t#** output_block)
672 {
673 lerc_assertcxx . . $ lerc_assert(b45ddcbc8423892c).(predictor == PREDICTOR_NONEcxx . . $ LercNS/PREDICTOR_NONE. || predictor == PREDICTOR_DELTA1cxx . . $ LercNS/PREDICTOR_DELTA1.);
674
675 size_t unit_size = output_buffers.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).();
676
677 lerc_assertcxx . . $ lerc_assert(b45ddcbc8423892c).(unit_size == UnitTypescxx . . $ LercNS/UnitTypes#::sizecxx . . $ LercNS/UnitTypes#size(5871b0c0ce9b5dff).(unit_type));
678
679 const int delta = Predictorcxx . . $ LercNS/Predictor#::getIntDeltacxx . . $ LercNS/Predictor#getIntDelta(d8ddbb2269178517).(predictor);
680
681 const size_t block_size = cols * rows;
682
683 uint8_tcxx . . $ uint8_t#* data_buffer = (uint8_tcxx . . $ uint8_t#*)malloc(block_size * unit_size);
684
685 if (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:416:9`! == data_buffer)
686 {
687 //perror("Out of memory.");
688 return false;
689 }
690
691 size_t offset = 0;
692
693 for (size_t i = 0; i < block_size; i++)
694 {
695 for (size_t byte = 0; byte < unit_size; byte++)
696 {
697 int idx = output_buffers[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).byte].firstcxx . . $ std/pair#first.;
698 data_buffer[idx + offset] = output_buffers[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).byte].secondcxx . . $ std/pair#second.[i];
699 }
700
701 offset += unit_size;
702 }
703
704 UnitTypescxx . . $ LercNS/UnitTypes#::restoreBlockSequencecxx . . $ LercNS/UnitTypes#restoreBlockSequence(ed9c1a8191464942).(delta, data_buffer, cols, rows, unit_type);
705
706 if (unit_type == UNIT_TYPE_FLOATcxx . . $ LercNS/UNIT_TYPE_FLOAT.)
707 {
708 UnitTypescxx . . $ LercNS/UnitTypes#::undoFloatTransformcxx . . $ LercNS/UnitTypes#undoFloatTransform(2769ff323d6644d4).((uint32_tcxx . . $ uint32_t#*)data_buffer, block_size);
709 }
710
711 if (output_block)
712 {
713 *output_block = data_buffer;
714 }
715 else
716 {
717 free(data_buffer); // Coverity warning fix. Currently, execution never gets here.
718 }
719
720 return true;
721 }
722
723 //////////////////////////////////////////////////////////////////////////////////////
724
725 bool LosslessFPCompressioncxx . . $ LercNS/LosslessFPCompression#::DecodeHuffmanFltcxx . . $ LercNS/LosslessFPCompression#DecodeHuffmanFlt(cd74ab201788fe3a).(const unsigned char** ppByte, size_t& nBytesRemainingInOut,
726 void* pData, bool bIsDouble, int iWidth, int iHeight, int iDepth)
727 {
728 if (iDepth == 1)
729 {
730 return DecodeHuffmanFltSlicecxx . . $ LercNS/LosslessFPCompression#DecodeHuffmanFltSlice(e4d5c94bff1049d2). (ppByte, nBytesRemainingInOut, pData, bIsDouble, iWidth, iHeight);
731 }
732 else
733 {
734 return DecodeHuffmanFltSlicecxx . . $ LercNS/LosslessFPCompression#DecodeHuffmanFltSlice(e4d5c94bff1049d2).(ppByte, nBytesRemainingInOut, pData, bIsDouble, iDepth, iWidth * iHeight);
735 }
736 }
737
738 bool LosslessFPCompressioncxx . . $ LercNS/LosslessFPCompression#::DecodeHuffmanFltSlicecxx . . $ LercNS/LosslessFPCompression#DecodeHuffmanFltSlice(e4d5c94bff1049d2). (const unsigned char** ppByte, size_t& nBytesRemainingInOut,
739 void * pData, bool bIsDouble, int iWidth, int iHeight)
740 {
741 unsigned char* ptr = (unsigned char *)(*ppByte);
742
743 UnitTypecxx . . $ LercNS/UnitType# unit_type = bIsDouble ? UNIT_TYPE_DOUBLEcxx . . $ LercNS/UNIT_TYPE_DOUBLE. : UNIT_TYPE_FLOATcxx . . $ LercNS/UNIT_TYPE_FLOAT.;
744
745 size_t bytes = UnitTypescxx . . $ LercNS/UnitTypes#::sizecxx . . $ LercNS/UnitTypes#size(5871b0c0ce9b5dff).(unit_type);
746
747 size_t expected_size = ((size_t)iWidth * iHeight);
748
749 stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::paircxx . . $ std/pair#<int, char*> >output_bufferscxx . . $ std/vector#vector(49f6e7a06ebc5aa8).;
750
751 unsigned char pred_code = 0;
752
753 memcpy(&pred_code, ptr, sizeof(pred_code));
754
755 if (pred_code > 2) // down the road, upper 6 bits can contain compression method.
756 // we support only Huffman in this version for now.
757 {
758 return false;
759 }
760
761 ptr += sizeof(pred_code);
762
763 nBytesRemainingInOut -= sizeof(pred_code);
764
765 for (size_t byte = 0; byte < bytes; byte++)
766 {
767 unsigned char byte_index = 0, best_level = 0;
768
769 uint32_tcxx . . $ uint32_t# compressed_size = 0;
770
771 if (nBytesRemainingInOut < 6)
772 return false;
773
774 memcpy(&byte_index, ptr, sizeof(byte_index));
775
776 if (byte_index >= bytes)
777 return false;
778
779 ptr += sizeof(byte_index);
780 nBytesRemainingInOut -= sizeof(byte_index);
781
782 memcpy(&best_level, ptr, sizeof(best_level));
783 ptr += sizeof(best_level);
784 nBytesRemainingInOut -= sizeof(best_level);
785
786 if (best_level > MAX_DELTAcxx . . $ `src/LercLib/fpl_Predictor.h:32:9`!)
787 return false;
788
789 memcpy(&compressed_size, ptr, sizeof(compressed_size));
790 ptr += sizeof(compressed_size);
791 nBytesRemainingInOut -= sizeof(compressed_size);
792
793 if (nBytesRemainingInOut < compressed_size)
794 return false;
795
796 unsigned char* compressed = (unsigned char *)malloc(compressed_size);
797
798 if (!compressed)
799 return false;
800
801 memcpy(compressed, ptr, compressed_size);
802 ptr += compressed_size;
803
804 nBytesRemainingInOut -= compressed_size;
805
806 char* uncompressed = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:416:9`!;
807
808 size_t extracted_size = fpl_Compressioncxx . . $ LercNS/fpl_Compression#::extract_buffercxx . . $ LercNS/fpl_Compression#extract_buffer(a437f2c8d8e82d1f).((const char *)compressed, compressed_size, expected_size, &uncompressed);
809
810 lerc_assertcxx . . $ lerc_assert(b45ddcbc8423892c).(expected_size == extracted_size);
811
812 free(compressed);
813 compressed = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:416:9`!;
814
815 int byte_delta = best_level;
816
817 unsigned char* restored = restoreSequencecxx . . $ restoreSequence(21b352d61946858b).((unsigned char*)uncompressed, extracted_size, byte_delta, false);
818
819 output_buffers.push_backcxx . . $ std/vector#push_back(fe7a8a4d1eddb70).(stdcxx . . $ std/pair#pair(f166319c93b32899).::make_paircxx . . $ std/make_pair(1d30b6e115203116).(byte_index, (char*)restored));
820 }
821
822 // nBytesRemainingInOut -= (ptr - *ppByte); => done above.
823 *ppByte = ptr;
824
825 PredictorTypecxx . . $ LercNS/PredictorType# predictor = Predictorcxx . . $ LercNS/Predictor#::getTypecxx . . $ LercNS/Predictor#getType(f57b6a48cece57f0).(pred_code);
826
827 const bool use_cross_delta = (predictor == PREDICTOR_ROWS_COLScxx . . $ LercNS/PREDICTOR_ROWS_COLS.);
828
829 uint8_tcxx . . $ uint8_t#* output_block_data = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:416:9`!;
830
831 bool ret = (predictor != PREDICTOR_UNKNOWNcxx . . $ LercNS/PREDICTOR_UNKNOWN.);
832
833 if (ret)
834 {
835 if (use_cross_delta)
836 {
837 ret = restoreCrossBytescxx . . $ restoreCrossBytes(847899b31443dfbf).(output_buffers, expected_size, iWidth, iHeight, predictor,
838 //NULL,
839 unit_type,
840 &output_block_data);
841 }
842 else
843 {
844 ret = restoreByteOrdercxx . . $ restoreByteOrder(1735502d4e909cf3).(output_buffers, iWidth, iHeight, predictor,
845 //NULL,
846 unit_type,
847 &output_block_data);
848 }
849 }
850
851 for (size_t i = 0; i < output_buffers.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).(); i++)
852 {
853 free(output_buffers[cxx . . $ std/vector#`operator[]`(81207c53da4b73ce).i].secondcxx . . $ std/pair#second.);
854 }
855
856 output_buffers.clearcxx . . $ std/vector#clear(ced63f7c635d850d).();
857
858 if (output_block_data) // ret is already set to false if memory was not allocated.
859 {
860 memcpy(pData, output_block_data, bytes * iWidth * iHeight);
861
862 free(output_block_data);
863 }
864
865 return (ret);
866 }