forked from VCG/compresso
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompresso.hpp
executable file
·1283 lines (1115 loc) · 35.5 KB
/
compresso.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This is an implementation of the Compresso
* segmentation compression codec. This
* is a heavily modified form of the code
* originally written by Brian Matejek.
*
* The stream written by this library is not
* compatible with the original version. It
* includes some byte width optimizations
* and additional header fields in the output
* and various functions have been somewhat
* tuned for speed. It also has a modified
* indeterminate locations algorithm to accomodate
* any possible input.
*
* You can find the Compresso paper here:
* https://vcg.seas.harvard.edu/publications/compresso-efficient-compression-of-segmentation-data-for-connectomics
*
* You can find the original code here:
* https://github.com/VCG/compresso/blob/8378346c9a189a48bf9054c5296ceeb7139634c5/experiments/compression/compresso/cpp-compresso.cpp
*
* William Silversmith
* Princeton University
* June 7, 2021
*/
#ifndef __COMPRESSO_HXX__
#define __COMPRESSO_HXX__
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <limits>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#include "cc3d.hpp"
namespace compresso {
#define DEFAULT_CONNECTIVITY 4
// little endian serialization of integers to chars
// returns bytes written
inline size_t itoc(uint8_t x, std::vector<unsigned char> &buf, size_t idx) {
buf[idx] = x;
return 1;
}
inline size_t itoc(uint16_t x, std::vector<unsigned char> &buf, size_t idx) {
buf[idx + 0] = x & 0xFF;
buf[idx + 1] = (x >> 8) & 0xFF;
return 2;
}
inline size_t itoc(uint32_t x, std::vector<unsigned char> &buf, size_t idx) {
buf[idx + 0] = x & 0xFF;
buf[idx + 1] = (x >> 8) & 0xFF;
buf[idx + 2] = (x >> 16) & 0xFF;
buf[idx + 3] = (x >> 24) & 0xFF;
return 4;
}
inline size_t itoc(uint64_t x, std::vector<unsigned char> &buf, size_t idx) {
buf[idx + 0] = x & 0xFF;
buf[idx + 1] = (x >> 8) & 0xFF;
buf[idx + 2] = (x >> 16) & 0xFF;
buf[idx + 3] = (x >> 24) & 0xFF;
buf[idx + 4] = (x >> 32) & 0xFF;
buf[idx + 5] = (x >> 40) & 0xFF;
buf[idx + 6] = (x >> 48) & 0xFF;
buf[idx + 7] = (x >> 56) & 0xFF;
return 8;
}
template <typename T>
T ctoi(unsigned char* buf, size_t idx = 0);
template <>
uint64_t ctoi(unsigned char* buf, size_t idx) {
uint64_t x = 0;
x += static_cast<uint64_t>(buf[idx + 0]) << 0;
x += static_cast<uint64_t>(buf[idx + 1]) << 8;
x += static_cast<uint64_t>(buf[idx + 2]) << 16;
x += static_cast<uint64_t>(buf[idx + 3]) << 24;
x += static_cast<uint64_t>(buf[idx + 4]) << 32;
x += static_cast<uint64_t>(buf[idx + 5]) << 40;
x += static_cast<uint64_t>(buf[idx + 6]) << 48;
x += static_cast<uint64_t>(buf[idx + 7]) << 56;
return x;
}
template <>
uint32_t ctoi(unsigned char* buf, size_t idx) {
uint32_t x = 0;
x += static_cast<uint32_t>(buf[idx + 0]) << 0;
x += static_cast<uint32_t>(buf[idx + 1]) << 8;
x += static_cast<uint32_t>(buf[idx + 2]) << 16;
x += static_cast<uint32_t>(buf[idx + 3]) << 24;
return x;
}
template <>
uint16_t ctoi(unsigned char* buf, size_t idx) {
uint16_t x = 0;
x += static_cast<uint16_t>(buf[idx + 0]) << 0;
x += static_cast<uint16_t>(buf[idx + 1]) << 8;
return x;
}
template <>
uint8_t ctoi(unsigned char* buf, size_t idx) {
return static_cast<uint8_t>(buf[idx]);
}
/* Header:
* 'cpso' : magic number (4 bytes)
* format version : unsigned integer (1 byte)
* data width : unsigned integer (1 byte) (1: uint8, ... 8: uint64)
* sx, sy, sz : size of each dimension (2 bytes x3)
* xstep,ystep,zstep : size of each grid (1 byte x 3) (typical values: 4, 8)
* id_size : number of uniq labels (u64) (could be one per voxel)
* value_size : number of values (u32)
* location_size : number of locations (u64)
* connectivity : CCL algorithm 4 or 6
*/
struct CompressoHeader {
public:
static constexpr size_t header_size{36};
static constexpr char magic[4]{ 'c', 'p', 's', 'o' };
uint8_t format_version; // 0: no z index ; 1: with z index
uint8_t data_width; // label width in bits
uint16_t sx;
uint16_t sy;
uint16_t sz;
uint8_t xstep; // 4 bits each to x and y (we only use 4 and 8 anyway)
uint8_t ystep; // 4 bits each to x and y (we only use 4 and 8 anyway)
uint8_t zstep; // 4 bits each to x and y (we only use 4 and 8 anyway)
uint64_t id_size; // label per connected component
uint32_t value_size; // boundary encodings (less than size / 16 or size / 64)
uint64_t location_size; // instructions to remap boundaries
uint8_t connectivity; // 4 or 6 connected CLL algorithm (almost always 4)
CompressoHeader() :
format_version(0), data_width(8),
sx(1), sy(1), sz(1),
xstep(8), ystep(8), zstep(1),
id_size(0), value_size(0), location_size(0),
connectivity(4)
{}
CompressoHeader(
const uint8_t _format_version, const uint8_t _data_width,
const uint16_t _sx, const uint16_t _sy, const uint16_t _sz,
const uint8_t _xstep = 4, const uint8_t _ystep = 4, const uint8_t _zstep = 1,
const uint64_t _id_size = 0, const uint32_t _value_size = 0,
const uint64_t _location_size = 0, const uint8_t _connectivity = 4
) :
format_version(_format_version),
data_width(_data_width),
sx(_sx), sy(_sy), sz(_sz),
xstep(_xstep), ystep(_ystep), zstep(_zstep),
id_size(_id_size), value_size(_value_size), location_size(_location_size),
connectivity(_connectivity)
{}
CompressoHeader(unsigned char* buf) {
bool valid_magic = (buf[0] == 'c' && buf[1] == 'p' && buf[2] == 's' && buf[3] == 'o');
format_version = buf[4];
if (!valid_magic || format_version > 1) {
throw std::runtime_error("compresso: Data stream is not valid. Unable to decompress.");
}
data_width = ctoi<uint8_t>(buf, 5);
sx = ctoi<uint16_t>(buf, 6);
sy = ctoi<uint16_t>(buf, 8);
sz = ctoi<uint16_t>(buf, 10);
xstep = ctoi<uint8_t>(buf, 12);
ystep = ctoi<uint8_t>(buf, 13);
zstep = ctoi<uint8_t>(buf, 14);
id_size = ctoi<uint64_t>(buf, 15);
value_size = ctoi<uint32_t>(buf, 23);
location_size = ctoi<uint64_t>(buf, 27);
connectivity = ctoi<uint8_t>(buf, 35);
if (data_width != 1 && data_width != 2 && data_width != 4 && data_width != 8) {
std::string err = "compresso: Invalid data width in stream. Unable to decompress. Got: ";
err += std::to_string(data_width);
throw std::runtime_error(err);
}
if (connectivity != 4 && connectivity != 6) {
std::string err = "compresso: Invalid connectivity in stream. Unable to decompress. Got: ";
err += std::to_string(connectivity);
throw std::runtime_error(err);
}
}
uint8_t index_byte_width() const {
const size_t sxy = sx * sy;
const size_t worst_case = 2 * sxy;
if (worst_case < std::numeric_limits<uint8_t>::max()) {
return 1;
}
else if (worst_case < std::numeric_limits<uint16_t>::max()) {
return 2;
}
else if (worst_case < std::numeric_limits<uint32_t>::max()) {
return 4;
}
else {
return 8;
}
}
size_t tochars(std::vector<unsigned char> &buf, size_t idx = 0) const {
if ((idx + CompressoHeader::header_size) > buf.size()) {
throw std::runtime_error("compresso: Unable to write past end of buffer.");
}
size_t i = idx;
for (int j = 0; j < 4; j++, i++) {
buf[i] = magic[j];
}
i += itoc(format_version, buf, i);
i += itoc(data_width, buf, i);
i += itoc(sx, buf, i);
i += itoc(sy, buf, i);
i += itoc(sz, buf, i);
i += itoc(xstep, buf, i);
i += itoc(ystep, buf, i);
i += itoc(zstep, buf, i);
i += itoc(id_size, buf, i);
i += itoc(value_size, buf, i);
i += itoc(location_size, buf, i);
i += itoc(connectivity, buf, i);
return i - idx;
}
static bool valid_header(unsigned char* buf) {
bool valid_magic = (buf[0] == 'c' && buf[1] == 'p' && buf[2] == 's' && buf[3] == 'o');
uint8_t format_version = buf[4];
uint8_t dwidth = ctoi<uint8_t>(buf, 5);
uint8_t connect = ctoi<uint8_t>(buf, 35);
bool valid_dtype = (dwidth == 1 || dwidth == 2 || dwidth == 4 || dwidth == 8);
bool valid_connectivity = (connect == 4 || connect == 6);
return valid_magic && (format_version < 2) && valid_dtype && valid_connectivity;
}
static CompressoHeader fromchars(unsigned char* buf) {
return CompressoHeader(buf);
}
};
template <typename LABEL>
bool* extract_boundaries(
LABEL *data,
const size_t sx, const size_t sy, const size_t sz,
const size_t connectivity
) {
const size_t sxy = sx * sy;
const size_t voxels = sxy * sz;
bool *boundaries = new bool[voxels]();
for (size_t z = 0; z < sz; z++) {
for (size_t y = 0; y < sy; y++) {
for (size_t x = 0; x < sx; x++) {
size_t loc = x + sx * y + sxy * z;
if (x < sx - 1 && data[loc] != data[loc + 1]) {
boundaries[loc] = true;
}
else if (y < sy - 1 && data[loc] != data[loc + sx]) {
boundaries[loc] = true;
}
else if (connectivity == 6 && z < sz - 1 && data[loc] != data[loc + sxy]) {
boundaries[loc] = true;
}
}
}
}
return boundaries;
}
template <typename T>
std::vector<T> component_map(
uint32_t *components, T *labels,
const size_t sx, const size_t sy, const size_t sz,
const size_t num_components
) {
const size_t sxy = sx * sy;
const size_t voxels = sxy * sz;
std::vector<T> ids(num_components);
for (size_t i = 0; i < voxels; i++) {
if (components[i] > 0) {
ids[components[i] - 1] = labels[i];
}
}
return ids;
}
template <typename T>
std::vector<T> encode_boundaries(
bool *boundaries,
const size_t sx, const size_t sy, const size_t sz,
const size_t xstep, const size_t ystep, const size_t zstep
) {
const size_t sxy = sx * sy;
const size_t nz = (sz + zstep - 1) / zstep; // round up
const size_t ny = (sy + ystep - 1) / ystep; // round up
const size_t nx = (sx + xstep - 1) / xstep; // round up
const size_t nblocks = nz * ny * nx;
std::vector<T> boundary_data(nblocks);
size_t xblock, yblock, zblock;
size_t xoffset, yoffset, zoffset;
// all these divisions can be replaced by plus/minus
for (size_t z = 0; z < sz; z++) {
zblock = z / zstep;
zoffset = z % zstep;
for (size_t y = 0; y < sy; y++) {
yblock = y / ystep;
yoffset = y % ystep;
for (size_t x = 0; x < sx; x++) {
size_t loc = x + sx * y + sxy * z;
if (!boundaries[loc]) {
continue;
}
xblock = x / xstep;
xoffset = x % xstep;
size_t block = xblock + nx * yblock + (ny * nx) * zblock;
size_t offset = xoffset + xstep * yoffset + (ystep * xstep) * zoffset;
boundary_data[block] += (static_cast<T>(1) << offset);
}
}
}
return boundary_data;
}
template <typename T>
std::vector<T> encode_indeterminate_locations(
bool* boundaries, T* labels,
const size_t sx, const size_t sy, const size_t sz,
const size_t connectivity,
std::vector<uint64_t> &z_index,
const bool random_access_z_index
) {
const size_t sxy = sx * sy;
std::vector<T> locations;
locations.reserve(sx * sy * sz / 10);
z_index.reserve(sz);
for (size_t z = 0; z < sz; z++) {
z_index.push_back(locations.size());
for (size_t y = 0; y < sy; y++) {
for (size_t x = 0; x < sx; x++) {
size_t loc = x + sx * y + sxy * z;
if (!boundaries[loc]) {
continue;
}
else if (x > 0 && !boundaries[loc - 1]) {
continue;
}
else if (y > 0 && !boundaries[loc - sx]) {
continue;
}
else if (connectivity == 6 && z > 0 && !boundaries[loc - sxy]) {
continue;
}
size_t left = loc - 1;
size_t right = loc + 1;
size_t up = loc - sx;
size_t down = loc + sx;
size_t heaven = loc - sxy;
size_t hell = loc + sxy;
// see if any of the immediate neighbors are candidates
if (x > 0 && !boundaries[left] && (labels[left] == labels[loc])) {
locations.push_back(0);
}
else if (x < sx - 1 && !boundaries[right] && (labels[right] == labels[loc])) {
locations.push_back(1);
}
else if (y > 0 && !boundaries[up] && (labels[up] == labels[loc])) {
locations.push_back(2);
}
else if (y < sy - 1 && !boundaries[down] && (labels[down] == labels[loc])) {
locations.push_back(3);
}
else if (!random_access_z_index && z > 0 && !boundaries[heaven] && (labels[heaven] == labels[loc])) {
locations.push_back(4);
}
else if (!random_access_z_index && z < sz - 1 && !boundaries[hell] && (labels[hell] == labels[loc])) {
locations.push_back(5);
}
else if (labels[loc] > std::numeric_limits<T>::max() - 7) {
locations.push_back(6);
locations.push_back(labels[loc]);
}
else {
locations.push_back(labels[loc] + 7);
}
}
}
}
// difference code the index positions
for (size_t i = sz - 1; i > 0; i--) {
z_index[i] -= z_index[i - 1];
}
return locations;
}
template <typename T>
std::vector<T> unique(const std::vector<T> &data) {
std::vector<T> values;
if (data.size() == 0) {
return values;
}
std::set<T> hash_map;
T last = data[0];
hash_map.insert(data[0]);
values.push_back(data[0]);
for (size_t iv = 1; iv < data.size(); iv++) {
if (data[iv] == last) {
continue;
}
bool inserted = hash_map.insert(data[iv]).second;
if (inserted) {
values.push_back(data[iv]);
}
last = data[iv];
}
sort(values.begin(), values.end());
return values;
}
template <typename T>
void renumber_boundary_data(const std::vector<T>& window_values, std::vector<T> &windows) {
if (windows.size() == 0) {
return;
}
std::unordered_map<T, T> mapping;
for (size_t iv = 0; iv < window_values.size(); iv++) {
mapping[window_values[iv]] = iv;
}
T last = windows[0];
windows[0] = mapping[windows[0]];
T last_remap = windows[0];
for (size_t iv = 1; iv < windows.size(); iv++) {
if (windows[iv] == last) {
windows[iv] = last_remap;
continue;
}
last_remap = mapping[windows[iv]];
last = windows[iv];
windows[iv] = last_remap;
}
}
template <>
void renumber_boundary_data(const std::vector<uint16_t>& window_values, std::vector<uint16_t> &windows) {
if (windows.size() == 0) {
return;
}
std::vector<uint16_t> mapping(pow(2,16));
for (size_t iv = 0; iv < window_values.size(); iv++) {
mapping[window_values[iv]] = iv;
}
for (size_t iv = 0; iv < windows.size(); iv++) {
windows[iv] = mapping[windows[iv]];
}
}
template <typename T>
std::vector<T> run_length_encode_windows(const std::vector<T> &windows) {
std::vector<T> rle_windows;
rle_windows.reserve(windows.size() / 4);
size_t zero_run = 0;
size_t max_run = std::numeric_limits<T>::max() / 2;
for (size_t i = 0; i < windows.size(); i++) {
if (windows[i] > max_run) {
throw std::runtime_error("compresso: Unable to RLE encode. Too many windows. Use 64-bit steps e.g. (8,8,1) instead.");
}
else if (windows[i] == 0) {
zero_run++;
if (zero_run == max_run) {
rle_windows.push_back((zero_run << 1) | 1);
zero_run = 0;
}
continue;
}
if (zero_run) {
rle_windows.push_back((zero_run << 1) | 1);
zero_run = 0;
}
rle_windows.push_back(windows[i] << 1);
}
if (zero_run) {
rle_windows.push_back((zero_run << 1) | 1);
}
return rle_windows;
}
template <typename WINDOW>
std::vector<WINDOW> run_length_decode_windows(
const std::vector<WINDOW> &rle_windows, const size_t nblocks
) {
std::vector<WINDOW> windows(nblocks);
WINDOW block = 0;
size_t index = 0;
const size_t window_size = rle_windows.size();
for (size_t i = 0; i < window_size; i++) {
block = rle_windows[i];
if (block & 1) {
index += (block >> 1);
}
else {
windows[index] = block >> 1;
index++;
}
}
return windows;
}
template <typename T>
void write_compressed_stream_index(
std::vector<unsigned char> &compressed_data,
size_t &idx,
const std::vector<uint64_t> &num_components_per_slice,
const std::vector<uint64_t> &z_index
) {
// random access z index at tail of file
for (size_t i = 0 ; i < num_components_per_slice.size(); i++) {
idx += itoc(static_cast<T>(num_components_per_slice[i]), compressed_data, idx);
}
for (size_t i = 0 ; i < z_index.size(); i++) {
idx += itoc(static_cast<T>(z_index[i]), compressed_data, idx);
}
}
template <typename LABEL, typename WINDOW>
void write_compressed_stream(
std::vector<unsigned char> &compressed_data,
const CompressoHeader &header,
const std::vector<LABEL> &ids,
const std::vector<WINDOW> &window_values,
const std::vector<LABEL> &locations,
const std::vector<WINDOW> &windows,
const std::vector<uint64_t> &num_components_per_slice,
const std::vector<uint64_t> &z_index,
const bool random_access_z_index
) {
size_t idx = header.tochars(compressed_data, 0);
for (size_t i = 0 ; i < ids.size(); i++) {
idx += itoc(ids[i], compressed_data, idx);
}
for (size_t i = 0 ; i < window_values.size(); i++) {
idx += itoc(window_values[i], compressed_data, idx);
}
for (size_t i = 0 ; i < locations.size(); i++) {
idx += itoc(locations[i], compressed_data, idx);
}
for (size_t i = 0 ; i < windows.size(); i++) {
idx += itoc(windows[i], compressed_data, idx);
}
if (random_access_z_index == false) {
return;
}
uint8_t index_width = header.index_byte_width();
if (index_width == 1) {
write_compressed_stream_index<uint8_t>(compressed_data, idx, num_components_per_slice, z_index);
}
else if (index_width == 2) {
write_compressed_stream_index<uint16_t>(compressed_data, idx, num_components_per_slice, z_index);
}
else if (index_width == 4) {
write_compressed_stream_index<uint32_t>(compressed_data, idx, num_components_per_slice, z_index);
}
else {
write_compressed_stream_index<uint64_t>(compressed_data, idx, num_components_per_slice, z_index);
}
}
template <typename LABEL, typename WINDOW>
std::vector<unsigned char> compress_helper(
LABEL* labels,
const size_t sx, const size_t sy, const size_t sz,
const size_t xstep, const size_t ystep, const size_t zstep,
const size_t connectivity, bool* boundaries,
const std::vector<LABEL>& ids,
const std::vector<uint64_t> &num_components_per_slice,
const bool random_access_z_index
) {
std::vector<uint64_t> z_index;
std::vector<WINDOW> windows = encode_boundaries<WINDOW>(boundaries, sx, sy, sz, xstep, ystep, zstep);
std::vector<LABEL> locations = encode_indeterminate_locations<LABEL>(
boundaries, labels, sx, sy, sz,
connectivity, z_index, random_access_z_index
);
delete[] boundaries;
std::vector<WINDOW> window_values = unique<WINDOW>(windows);
renumber_boundary_data(window_values, windows);
windows = run_length_encode_windows<WINDOW>(windows);
CompressoHeader header(
/*format_version=*/static_cast<uint8_t>(random_access_z_index),
/*data_width=*/sizeof(LABEL),
/*sx=*/sx, /*sy=*/sy, /*sz=*/sz,
/*xstep=*/xstep, /*ystep=*/ystep, /*zstep=*/zstep,
/*id_size=*/ids.size(),
/*value_size=*/window_values.size(),
/*location_size=*/locations.size(),
/*connectivity=*/connectivity
);
size_t index_width = static_cast<size_t>(header.index_byte_width());
size_t num_out_bytes = (
CompressoHeader::header_size
+ (ids.size() * sizeof(LABEL))
+ (window_values.size() * sizeof(WINDOW))
+ (locations.size() * sizeof(LABEL))
+ (windows.size() * sizeof(WINDOW))
+ (num_components_per_slice.size() * index_width * random_access_z_index)
+ (z_index.size() * index_width * random_access_z_index)
);
std::vector<unsigned char> compressed_data(num_out_bytes);
write_compressed_stream<LABEL, WINDOW>(
compressed_data, header, ids,
window_values, locations, windows,
num_components_per_slice, z_index,
random_access_z_index
);
return compressed_data;
}
std::vector<unsigned char> zero_data_stream(
const size_t sx, const size_t sy, const size_t sz,
const size_t xstep, const size_t ystep, const size_t zstep,
const size_t data_width, const size_t connectivity
) {
std::vector<unsigned char> compressed_data(CompressoHeader::header_size);
CompressoHeader empty_header(
/*format_version=*/0,
/*data_width=*/data_width,
/*sx=*/sx, /*sy=*/sy, /*sz=*/sz,
/*xstep=*/xstep, /*ystep=*/ystep, /*zstep=*/zstep,
/*id_size=*/0,
/*value_size=*/0,
/*location_size=*/0,
/*connectivity*/connectivity
);
empty_header.tochars(compressed_data);
return compressed_data;
}
/* compress
*
* Convert 3D integer array data into a compresso encoded byte stream.
* Array is expected to be in Fortran order.
*
* Parameters:
* data: pointer to 3D integer segmentation image
* sx, sy, sz: axial dimension sizes
* xstep, ystep, zstep: (optional) picks the size of the
* compresso grid. 4x4x1 or 8x8x1 are acceptable sizes.
* connectivity: 4 (2d) or 6 (3d)
* random_access_z_index: avoids encoding dependencies between
* z layers and adds an index to the end of the stream to
* enable decoding of slices independently. May increase compressed
* file size (usually by very little). Only supported with
* connectivity=4.
*
* Returns: vector<char>
*/
template <typename T>
std::vector<unsigned char> compress(
T* labels,
const size_t sx, const size_t sy, const size_t sz,
const size_t xstep = 4, const size_t ystep = 4, const size_t zstep = 1,
const size_t connectivity = 4, const bool random_access_z_index = true
) {
if (sx * sy * sz == 0) {
return zero_data_stream(sx, sy, sz, xstep, ystep, zstep, sizeof(T), connectivity);
}
if (xstep * ystep * zstep > 64) {
throw std::runtime_error("compresso: Unable to encode blocks larger than 64 voxels.");
}
else if (xstep * ystep * zstep == 0) {
throw std::runtime_error("compresso: Unable to encode using zero step sizes.");
}
if (connectivity == 6 && random_access_z_index) {
throw std::runtime_error("compresso: Random access index not supported with connectivity 6.");
}
bool *boundaries = extract_boundaries<T>(labels, sx, sy, sz, connectivity);
size_t num_components = 0;
std::vector<uint64_t> num_components_per_slice(sz);
uint32_t *components = cc3d::connected_components<uint32_t>(
boundaries, sx, sy, sz,
num_components_per_slice,
/*connectivity=*/connectivity, num_components
);
std::vector<T> ids = component_map<T>(components, labels, sx, sy, sz, num_components);
delete[] components;
// can use a more efficient window size
// if the grid size is small enough.
// specifically, we're talking about
// 4x4x1 step size
if (xstep * ystep * zstep <= 8) {
return compress_helper<T, uint8_t>(
labels,
sx, sy, sz,
xstep, ystep, zstep, connectivity,
boundaries, ids, num_components_per_slice,
random_access_z_index
);
}
else if (xstep * ystep * zstep <= 16) {
return compress_helper<T, uint16_t>(
labels,
sx, sy, sz,
xstep, ystep, zstep, connectivity,
boundaries, ids, num_components_per_slice,
random_access_z_index
);
}
else if (xstep * ystep * zstep <= 32) { // 4x4x2 for example
return compress_helper<T, uint32_t>(
labels,
sx, sy, sz,
xstep, ystep, zstep, connectivity,
boundaries, ids, num_components_per_slice,
random_access_z_index
);
}
else { // for 8x8x1 step size
return compress_helper<T, uint64_t>(
labels,
sx, sy, sz,
xstep, ystep, zstep, connectivity,
boundaries, ids, num_components_per_slice,
random_access_z_index
);
}
}
/* DECOMPRESS STARTS HERE */
template <typename LABEL, typename WINDOW>
bool* decode_boundaries(
const std::vector<WINDOW> &windows, const std::vector<WINDOW> &window_values,
const size_t sx, const size_t sy, const size_t sz,
const size_t xstep, const size_t ystep, const size_t zstep,
const size_t zstart, const size_t zend
) {
const size_t sxy = sx * sy;
const size_t nx = (sx + xstep - 1) / xstep; // round up
const size_t ny = (sy + ystep - 1) / ystep; // round up
// check for power of two
const bool xstep_pot = (xstep != 0) && ((xstep & (xstep - 1)) == 0);
const int xshift = std::log2(xstep); // must use log2 here, not lg/lg2 to avoid fp errors
bool* boundaries = new bool[sxy * (zend - zstart)]();
if (window_values.size() == 0) {
return boundaries;
}
size_t xblock, yblock, zblock;
size_t xoffset, yoffset, zoffset;
for (size_t z = zstart; z < zend; z++) {
zblock = nx * ny * (z / zstep);
zoffset = xstep * ystep * (z % zstep);
for (size_t y = 0; y < sy; y++) {
yblock = nx * (y / ystep);
yoffset = xstep * (y % ystep);
if (xstep_pot) {
for (size_t x = 0; x < sx; x++) {
size_t iv = x + sx * y + sxy * (z - zstart);
xblock = x >> xshift; // x / xstep
xoffset = x & ((1 << xshift) - 1); // x % xstep
size_t block = xblock + yblock + zblock;
size_t offset = xoffset + yoffset + zoffset;
WINDOW value = window_values[windows[block]];
boundaries[iv] = (value >> offset) & 0b1;
}
}
else {
for (size_t x = 0; x < sx; x++) {
size_t iv = x + sx * y + sxy * (z - zstart);
xblock = x / xstep;
xoffset = x % xstep;
size_t block = xblock + yblock + zblock;
size_t offset = xoffset + yoffset + zoffset;
WINDOW value = window_values[windows[block]];
boundaries[iv] = (value >> offset) & 0b1;
}
}
}
}
return boundaries;
}
template <typename LABEL>
void decode_nonboundary_labels(
uint32_t *components, const std::vector<LABEL> &ids,
const size_t sx, const size_t sy, const size_t sz,
LABEL* output
) {
const size_t voxels = sx * sy * sz;
for (size_t i = 0; i < voxels; i++) {
output[i] = ids[components[i]];
}
}
template <typename LABEL>
void decode_indeterminate_locations(
bool *boundaries, LABEL *labels,
const std::vector<LABEL> &locations,
const size_t sx, const size_t sy, const size_t sz,
const size_t connectivity,
const size_t zstart, const size_t zend,
const std::vector<uint64_t> &z_index
) {
const size_t sxy = sx * sy;
size_t loc = 0;
size_t index = z_index[zstart];
// go through all coordinates
for (size_t z = zstart; z < zend; z++) {
for (size_t y = 0; y < sy; y++) {
for (size_t x = 0; x < sx; x++) {
size_t zoff = z - zstart;
loc = x + sx * y + sxy * zoff;
if (!boundaries[loc]) {
continue;
}
else if (x > 0 && !boundaries[loc - 1]) {
labels[loc] = labels[loc - 1];
continue;
}
else if (y > 0 && !boundaries[loc - sx]) {
labels[loc] = labels[loc - sx];
continue;
}
else if (connectivity == 6 && zoff > 0 && !boundaries[loc - sxy]) {
labels[loc] = labels[loc - sxy];
continue;
}
else if (locations.size() == 0) {
throw std::runtime_error("compresso: unable to decode indeterminate locations. (no locations)");
}
size_t offset = locations[index];
if (offset == 0) {
if (x == 0) {
throw std::runtime_error("compresso: unable to decode indeterminate locations. (offset 0)");
}
labels[loc] = labels[loc - 1];
}
else if (offset == 1) {
if (x >= sx - 1) {
throw std::runtime_error("compresso: unable to decode indeterminate locations. (offset 1)");
}
labels[loc] = labels[loc + 1];
}
else if (offset == 2) {
if (y == 0) {
throw std::runtime_error("compresso: unable to decode indeterminate locations. (offset 2)");
}
labels[loc] = labels[loc - sx];
}
else if (offset == 3) {
if (y >= sy - 1) {
throw std::runtime_error("compresso: unable to decode indeterminate locations. (offset 3)");
}
labels[loc] = labels[loc + sx];
}
else if (offset == 4) {
if (zoff == 0) {
throw std::runtime_error("compresso: unable to decode indeterminate locations. (offset 4)");
}
labels[loc] = labels[loc - sxy];
}
else if (offset == 5) {
if (zoff >= zend) {
throw std::runtime_error("compresso: unable to decode indeterminate locations. (offset 5)");
}
labels[loc] = labels[loc + sxy];
}
else if (offset == 6) {
labels[loc] = locations[index + 1];
index++;
}
else {
labels[loc] = offset - 7;
}
index++;
}
}
}
}
template <typename T>
void decode_z_index(
unsigned char* buffer, size_t sz, size_t iv,
std::vector<uint64_t> &components_index,
std::vector<uint64_t> &z_index
) {
for (size_t ix = 0; ix < sz; ix++, iv += sizeof(T)) {
components_index[ix] = static_cast<uint64_t>(
ctoi<T>(buffer, iv)
);
}
for (size_t ix = 0; ix < sz; ix++, iv += sizeof(T)) {
z_index[ix] = static_cast<uint64_t>(
ctoi<T>(buffer, iv)
);
}
// decode difference coded indeterminate locations index
for (size_t i = 1; i < sz; i++) {
z_index[i] += z_index[i-1];
components_index[i] += components_index[i-1];
}
}