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"
26 #include "fpl_Compression.h"
27 #include <assert.h>
28 #include <cmath>
29 #include <cstring>
30 #include <stdint.h>
31 #include <algorithm>
32
33 USING_NAMESPACE_LERC
34
35 void lerc_assert(bool v)
36 {
37 if (v == false)
38 throw "Assertion failed";
39 }
40
41 template<typename T>
42 size_t getMinIndex(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 TestBlock
60 {
61 long top, height;
62 };
63
64 static void generateListOfTestBlocks(const int width, const int height, std::vector <TestBlock>& blocks)
65 {
66 size_t size = (size_t)width * height;
67
68 lerc_assert(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 TestBlock tb;
92
93 tb.top = (long)(top_margin + delta * i);
94 tb.height = block_height;
95 if (tb.top < 0) tb.top = 0;
96 if (tb.top + tb.height > height) tb.height = height - tb.top;
97
98 if (tb.height > 0)
99 blocks.push_back(tb);
100 }
101 }
102
103 static void setDerivativePrime(unsigned char* data, size_t size)
104 {
105 int off = (int)size - 1;
106
107 off = PRIME_MULT * (off / PRIME_MULT);
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_MULT;
115 }
116 }
117
118 static void setDerivative(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* restoreSequence(unsigned char* data, size_t size, const int level, bool make_copy)
134 {
135 assert(size > 0);
136
137 unsigned char* copy = NULL;
138
139 if (make_copy)
140 {
141 copy = (unsigned char*)malloc(size);
142
143 if (!copy)
144 {
145 //perror("Out of memory.");
146 return NULL;
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 testBlocksSize(std::vector <TestBlock>& blocks, const 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_t* data = (const uint8_t*)_data;
176
177 const size_t unit_size = UnitTypes::size(unit_type);
178
179 for (size_t blk = 0; blk < blocks.size(); blk++)
180 {
181 TestBlock& tb = blocks[blk];
182
183 size_t start = unit_size * tb.top * raster_width;
184 size_t length = tb.height * raster_width;
185
186 // input_len += length * unit_size; //sizeof(uint32_t);
187
188 char* plane_buffer = (char*)malloc(length);
189
190 assert(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_Compression::compress_buffer((const char*)plane_buffer, length, NULL, 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 setDerivativePrime((unsigned char*)plane_buffer, length);
214
215 size_t plane_encoded2 = fpl_Compression::compress_buffer((const char*)plane_buffer, length, NULL, true);
216
217 ret += (std::min)(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 getBestLevel2(const unsigned char* ptr, size_t size, const int max_delta_order)
236 {
237 std::vector<std::pair<size_t, int> > snippets;
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 assert(len > 0);
263
264 if (len > 0)
265 snippets.push_back(std::make_pair(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.size(); s++)
284 {
285 size_t start = snippets[s].first;
286 int len = snippets[s].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.size(); i++)
298 {
299 size_t start = snippets[i].first;
300 int len = snippets[i].second;
301
302 comp += fpl_Compression::compress_buffer((const char*)copy + start, len, NULL, 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 getBestLevel(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 getBestLevel2(ptr, size, max_delta_order);
329 }
330
331 ///////////////////////////////////////////////////////////////////////////////////////////////
332
333 LosslessFPCompression::~LosslessFPCompression()
334 {
335 delete m_data_slice;
336 }
337
338 void LosslessFPCompression::selectInitialLinearOrCrossDelta(const 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 std::vector <TestBlock> blocks;
342
343 size_t local_stats[3] = { 0 };
344
345 if (stats == NULL)
346 {
347 stats = local_stats;
348 }
349
350 generateListOfTestBlocks(iWidth, iHeight, blocks);
351
352 size_t est = 0;
353
354 // unchanged block:
355 est = testBlocksSize(blocks, unit_type, pData, iWidth, test_first_byte_delta);
356
357 if (stats) { stats[0] += est; }
358
359 // linear part:
360
361 UnitTypes::setBlockDerivative(unit_type, pData, iWidth, iHeight, 1, 1);
362
363 est = testBlocksSize(blocks, unit_type, pData, iWidth, test_first_byte_delta);
364
365 if (stats) { stats[1] += est; }
366
367 UnitTypes::setCrossDerivative(unit_type, pData, iWidth, iHeight, 2, 2);
368
369 est = testBlocksSize(blocks, unit_type, pData, iWidth, test_first_byte_delta);
370
371 if (stats) { stats[2] += est; }
372
373 size_t min_index = getMinIndex<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 LosslessFPCompression::compressedLength() const
393 {
394 int ret = 1; // predictor code
395
396 for (auto b : m_data_slice->m_buffers)
397 {
398 ret += b->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 LosslessFPCompression::EncodeHuffmanFlt(unsigned char ** ppByte)
406 {
407 memcpy(*ppByte, &(m_data_slice->m_predictor_code), sizeof(m_data_slice->m_predictor_code));
408 *ppByte += sizeof(m_data_slice->m_predictor_code);
409
410 for (auto b : m_data_slice->m_buffers)
411 {
412 memcpy(*ppByte, &(b->byte_index), sizeof(b->byte_index));
413 *ppByte += sizeof(b->byte_index);
414 memcpy(*ppByte, &(b->best_level), sizeof(b->best_level));
415 *ppByte += sizeof(b->best_level);
416 memcpy(*ppByte, &(b->compressed_size), sizeof(b->compressed_size));
417 *ppByte += sizeof(b->compressed_size);
418 memcpy(*ppByte, b->compressed, b->compressed_size);
419 *ppByte += b->compressed_size;
420 }
421
422 for (auto v : m_data_slice->m_buffers)
423 {
424 delete v;
425 }
426
427 m_data_slice->m_buffers.clear();
428
429 return true;
430 }
431
432 bool LosslessFPCompression::ComputeHuffmanCodesFlt(const void* input, bool bIsDouble,
433 int iCols, int iRows, int iDepth)
434 {
435 if (iDepth == 1)
436 {
437 if (m_data_slice && !m_data_slice->m_buffers.empty())
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_slice->m_buffers)
442 {
443 delete v;
444 }
445
446 m_data_slice->m_buffers.clear();
447 }
448 return ComputeHuffmanCodesFltSlice (input, bIsDouble, iCols, iRows);
449 }
450 else
451 {
452 return ComputeHuffmanCodesFltSlice(input, bIsDouble, iDepth, iCols * iRows);
453 }
454 }
455
456 bool LosslessFPCompression::ComputeHuffmanCodesFltSlice (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 UnitType unit_type = bIsDouble ? UNIT_TYPE_DOUBLE : UNIT_TYPE_FLOAT;
467
468 int max_byte_delta = MAX_DELTA; // 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 = UnitTypes::size(unit_type);
475 size_t block_size = size;
476
477 uint8_t* block_values = (uint8_t*)malloc(block_size * unit_size);
478
479 memcpy(block_values, pInput, block_size * unit_size);
480
481 if (unit_type == UNIT_TYPE_FLOAT)
482 UnitTypes::doFloatTransform((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_t* copy = (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 selectInitialLinearOrCrossDelta(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 = getMinIndex<size_t>(stats1, 3);
508
509 PredictorType predictor = PREDICTOR_NONE;
510
511 if (min_index < 2)
512 {
513 if (min_index == 1) predictor = PREDICTOR_DELTA1;
514 }
515 else
516 {
517 if (min_index == 2) predictor = PREDICTOR_ROWS_COLS;
518 }
519
520 int w = block_width, h = block_height;
521
522 if (predictor == PREDICTOR_ROWS_COLS)
523 {
524 if (predictor == PREDICTOR_ROWS_COLS)
525 UnitTypes::setCrossDerivative(unit_type, block_values, w, h, 2);
526 }
527 else
528 {
529 int delta = 0;
530
531 if (predictor == PREDICTOR_DELTA1) delta = 1;
532
533 UnitTypes::setBlockDerivative(unit_type, block_values, w, h, delta);
534 }
535
536 int max_delta = Predictor::getMaxByteDelta(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_slice)
551 m_data_slice = new 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 = getBestLevel(block_buff, block_size, max_delta);
573
574 setDerivative(block_buff, block_size, bestLevel);
575
576 char* compressed = NULL;
577
578 size_t ret = fpl_Compression::compress_buffer((const char*)block_buff, block_size, &compressed);
579
580 if (ret > UINT32_MAX) // 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_slice->m_predictor_code = Predictor::getCode(predictor);
592
593 {
594 outBlockBuffer* ob = new outBlockBuffer;
595 ob->compressed = compressed;
596 ob->compressed_size = (uint32_t)ret;
597 ob->byte_index = (unsigned char)byte;
598 ob->best_level = (unsigned char)bestLevel;
599
600 m_data_slice->m_buffers.push_back(ob);
601 }
602 }
603
604 free(block_buff);
605 free(block_values);
606
607 return true;
608 }
609
610 /////////////////////////////////////////////////////////////////////////////////////////////////
611
612 bool restoreCrossBytes(std::vector<std::pair<int, char*> >& output_buffers, const size_t input_in_bytes,
613 const size_t cols, const size_t rows, const PredictorType predictor,
614 //FILE* output,
615 const UnitType unit_type, uint8_t** output_block)
616 {
617 lerc_assert(predictor == PREDICTOR_NONE || predictor == PREDICTOR_ROWS_COLS);
618
619 size_t unit_size = output_buffers.size();
620
621 lerc_assert(unit_size == UnitTypes::size(unit_type));
622
623 const int delta = Predictor::getIntDelta(predictor);
624
625 size_t block_size = cols * rows;
626
627 assert(input_in_bytes == block_size);
628
629 uint8_t* data_buffer = (uint8_t*)malloc(block_size * unit_size);
630
631 if (NULL == 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[byte].first;
644 data_buffer[idx + offset] = output_buffers[byte].second[i];
645 }
646
647 offset += unit_size;
648 }
649
650 UnitTypes::restoreCrossBytes(delta, data_buffer, cols, rows, unit_type);
651
652 if (unit_type == UNIT_TYPE_FLOAT)
653 {
654 UnitTypes::undoFloatTransform((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 restoreByteOrder(std::vector<std::pair<int, char*> >& output_buffers,
670 const size_t cols, const size_t rows, const PredictorType predictor, //FILE* output,
671 const UnitType unit_type, uint8_t** output_block)
672 {
673 lerc_assert(predictor == PREDICTOR_NONE || predictor == PREDICTOR_DELTA1);
674
675 size_t unit_size = output_buffers.size();
676
677 lerc_assert(unit_size == UnitTypes::size(unit_type));
678
679 const int delta = Predictor::getIntDelta(predictor);
680
681 const size_t block_size = cols * rows;
682
683 uint8_t* data_buffer = (uint8_t*)malloc(block_size * unit_size);
684
685 if (NULL == 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[byte].first;
698 data_buffer[idx + offset] = output_buffers[byte].second[i];
699 }
700
701 offset += unit_size;
702 }
703
704 UnitTypes::restoreBlockSequence(delta, data_buffer, cols, rows, unit_type);
705
706 if (unit_type == UNIT_TYPE_FLOAT)
707 {
708 UnitTypes::undoFloatTransform((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 LosslessFPCompression::DecodeHuffmanFlt(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 DecodeHuffmanFltSlice (ppByte, nBytesRemainingInOut, pData, bIsDouble, iWidth, iHeight);
731 }
732 else
733 {
734 return DecodeHuffmanFltSlice(ppByte, nBytesRemainingInOut, pData, bIsDouble, iDepth, iWidth * iHeight);
735 }
736 }
737
738 bool LosslessFPCompression::DecodeHuffmanFltSlice (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 UnitType unit_type = bIsDouble ? UNIT_TYPE_DOUBLE : UNIT_TYPE_FLOAT;
744
745 size_t bytes = UnitTypes::size(unit_type);
746
747 size_t expected_size = ((size_t)iWidth * iHeight);
748
749 std::vector<std::pair<int, char*> >output_buffers;
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_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_DELTA)
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 = NULL;
807
808 size_t extracted_size = fpl_Compression::extract_buffer((const char *)compressed, compressed_size, expected_size, &uncompressed);
809
810 lerc_assert(expected_size == extracted_size);
811
812 free(compressed);
813 compressed = NULL;
814
815 int byte_delta = best_level;
816
817 unsigned char* restored = restoreSequence((unsigned char*)uncompressed, extracted_size, byte_delta, false);
818
819 output_buffers.push_back(std::make_pair(byte_index, (char*)restored));
820 }
821
822 // nBytesRemainingInOut -= (ptr - *ppByte); => done above.
823 *ppByte = ptr;
824
825 PredictorType predictor = Predictor::getType(pred_code);
826
827 const bool use_cross_delta = (predictor == PREDICTOR_ROWS_COLS);
828
829 uint8_t* output_block_data = NULL;
830
831 bool ret = (predictor != PREDICTOR_UNKNOWN);
832
833 if (ret)
834 {
835 if (use_cross_delta)
836 {
837 ret = restoreCrossBytes(output_buffers, expected_size, iWidth, iHeight, predictor,
838 //NULL,
839 unit_type,
840 &output_block_data);
841 }
842 else
843 {
844 ret = restoreByteOrder(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.size(); i++)
852 {
853 free(output_buffers[i].second);
854 }
855
856 output_buffers.clear();
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 }