-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
507 lines (417 loc) · 13.6 KB
/
util.h
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
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#include "volcano.h"
#include "snic.h"
#include "chord.h"
#define ZLIB_CHUNK_SIZE 16384
// Helper function to compress a string using zlib
static int compress_string(const char* input, size_t input_len, char** output, size_t* output_len) {
z_stream strm;
char out[ZLIB_CHUNK_SIZE];
char* compressed = NULL;
size_t total_size = 0;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
if (deflateInit2(&strm, Z_BEST_COMPRESSION, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
return -1;
}
strm.avail_in = input_len;
strm.next_in = (unsigned char*)input;
do {
strm.avail_out = ZLIB_CHUNK_SIZE;
strm.next_out = (unsigned char*)out;
if (deflate(&strm, Z_FINISH) == Z_STREAM_ERROR) {
deflateEnd(&strm);
free(compressed);
return -1;
}
size_t have = ZLIB_CHUNK_SIZE - strm.avail_out;
char* new_compressed = realloc(compressed, total_size + have);
if (!new_compressed) {
deflateEnd(&strm);
free(compressed);
return -1;
}
compressed = new_compressed;
memcpy(compressed + total_size, out, have);
total_size += have;
} while (strm.avail_out == 0);
deflateEnd(&strm);
*output = compressed;
*output_len = total_size;
return 0;
}
// Helper function to decompress a string using zlib
static int decompress_string(const char* input, size_t input_len, char** output, size_t* output_len) {
z_stream strm;
char out[ZLIB_CHUNK_SIZE];
char* decompressed = NULL;
size_t total_size = 0;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
if (inflateInit2(&strm, 31) != Z_OK) {
return -1;
}
strm.avail_in = input_len;
strm.next_in = (unsigned char*)input;
do {
strm.avail_out = ZLIB_CHUNK_SIZE;
strm.next_out = (unsigned char*)out;
int ret = inflate(&strm, Z_NO_FLUSH);
if (ret != Z_OK && ret != Z_STREAM_END) {
inflateEnd(&strm);
free(decompressed);
return -1;
}
size_t have = ZLIB_CHUNK_SIZE - strm.avail_out;
char* new_decompressed = realloc(decompressed, total_size + have);
if (!new_decompressed) {
inflateEnd(&strm);
free(decompressed);
return -1;
}
decompressed = new_decompressed;
memcpy(decompressed + total_size, out, have);
total_size += have;
} while (strm.avail_out == 0);
inflateEnd(&strm);
*output = decompressed;
*output_len = total_size;
return 0;
}
// Save superpixels to compressed CSV
static int superpixels_to_compressed_csv(char* path, Superpixel* superpixels, int num_superpixels) {
// First, write to memory
char* csv_data = NULL;
size_t csv_size = 0;
FILE* memfile = open_memstream(&csv_data, &csv_size);
if (!memfile) return -1;
// Write header
fprintf(memfile, "z,y,x,intensity,pixel_count\n");
// Write data
for (int i = 0; i < num_superpixels; i++) {
fprintf(memfile, "%.1f,%.1f,%.1f,%.1f,%u\n",
superpixels[i].z,
superpixels[i].y,
superpixels[i].x,
superpixels[i].c,
superpixels[i].n);
}
fclose(memfile);
// Compress the data
char* compressed_data;
size_t compressed_size;
if (compress_string(csv_data, csv_size, &compressed_data, &compressed_size) != 0) {
free(csv_data);
return -1;
}
// Write compressed data to file
FILE* fp = fopen(path, "wb");
if (!fp) {
free(csv_data);
free(compressed_data);
return -1;
}
size_t written = fwrite(compressed_data, 1, compressed_size, fp);
fclose(fp);
free(csv_data);
free(compressed_data);
return written == compressed_size ? 0 : -1;
}
// Load superpixels from compressed CSV
static Superpixel* compressed_csv_to_superpixels(char* path, int* num_superpixels_out) {
// Read compressed file
FILE* fp = fopen(path, "rb");
if (!fp) return NULL;
fseek(fp, 0, SEEK_END);
size_t file_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
char* compressed_data = malloc(file_size);
if (!compressed_data) {
fclose(fp);
return NULL;
}
if (fread(compressed_data, 1, file_size, fp) != file_size) {
free(compressed_data);
fclose(fp);
return NULL;
}
fclose(fp);
// Decompress data
char* csv_data;
size_t csv_size;
if (decompress_string(compressed_data, file_size, &csv_data, &csv_size) != 0) {
free(compressed_data);
return NULL;
}
free(compressed_data);
// Parse CSV from memory
char* line = csv_data;
char* end = csv_data + csv_size;
int num_lines = 0;
// Skip header
while (line < end && *line != '\n') line++;
line++; // Skip the newline
// Count lines
char* counting_line = line;
while (counting_line < end) {
if (*counting_line++ == '\n') num_lines++;
}
// Allocate array
Superpixel* superpixels = calloc(num_lines, sizeof(Superpixel));
if (!superpixels) {
free(csv_data);
return NULL;
}
// Read data
int i = 0;
while (line < end && i < num_lines) {
float z, y, x, c;
unsigned int n;
if (sscanf(line, "%f,%f,%f,%f,%u", &z, &y, &x, &c, &n) == 5) {
superpixels[i].z = z;
superpixels[i].y = y;
superpixels[i].x = x;
superpixels[i].c = c;
superpixels[i].n = n;
i++;
}
// Move to next line
while (line < end && *line != '\n') line++;
line++; // Skip the newline
}
free(csv_data);
*num_superpixels_out = num_lines;
return superpixels;
}
// Save superpixels to CSV
static int superpixels_to_csv(char* path, Superpixel* superpixels, int num_superpixels) {
FILE* fp = fopen(path, "w");
if (!fp) return -1;
// Write header
fprintf(fp, "z,y,x,intensity,pixel_count\n");
// Write data
for (int i = 0; i < num_superpixels; i++) {
fprintf(fp, "%.1f,%.1f,%.1f,%.1f,%u\n",
superpixels[i].z,
superpixels[i].y,
superpixels[i].x,
superpixels[i].c,
superpixels[i].n);
}
fclose(fp);
return 0;
}
// Load superpixels from CSV
static Superpixel* csv_to_superpixels(char* path, int* num_superpixels_out) {
FILE* fp = fopen(path, "r");
if (!fp) return NULL;
char line[1024];
int num_lines = 0;
// Skip header
fgets(line, sizeof(line), fp);
// Count lines
while (fgets(line, sizeof(line), fp)) {
num_lines++;
}
// Allocate array
Superpixel* superpixels = calloc(num_lines, sizeof(Superpixel));
if (!superpixels) {
fclose(fp);
return NULL;
}
// Reset file pointer and skip header again
fseek(fp, 0, SEEK_SET);
fgets(line, sizeof(line), fp);
// Read data
int i = 0;
while (fgets(line, sizeof(line), fp)) {
float z, y, x, c;
unsigned int n;
if (sscanf(line, "%f,%f,%f,%f,%u",
&z, &y, &x, &c, &n) == 5) {
superpixels[i].z = z;
superpixels[i].y = y;
superpixels[i].x = x;
superpixels[i].c = c;
superpixels[i].n = n;
i++;
}
}
fclose(fp);
*num_superpixels_out = num_lines;
return superpixels;
}
// Save chords to CSV - just saves the list of superpixel indices
static int chords_to_csv(char* path, Chord* chords, int num_chords) {
FILE* fp = fopen(path, "w");
if (!fp) return -1;
// Write header
fprintf(fp, "points\n");
// Write data - each line is a comma-separated list of points
for (int i = 0; i < num_chords; i++) {
for (int j = 0; j < chords[i].point_count; j++) {
fprintf(fp, "%u", chords[i].points[j]);
if (j < chords[i].point_count - 1) fprintf(fp, ",");
}
fprintf(fp, "\n");
}
fclose(fp);
return 0;
}
// Load chords from CSV
static Chord* csv_to_chords(char* path, int* num_chords_out) {
FILE* fp = fopen(path, "r");
if (!fp) return NULL;
char line[16384]; // Large buffer for long point lists
int num_lines = 0;
// Skip header
fgets(line, sizeof(line), fp);
// Count lines
while (fgets(line, sizeof(line), fp)) {
num_lines++;
}
// Allocate array
Chord* chords = calloc(num_lines, sizeof(Chord));
if (!chords) {
fclose(fp);
return NULL;
}
// Reset file pointer and skip header
fseek(fp, 0, SEEK_SET);
fgets(line, sizeof(line), fp);
// Read data
int i = 0;
while (fgets(line, sizeof(line), fp)) {
// First pass: count points in this line
int point_count = 1; // Start at 1 for the first number
for (char* c = line; *c; c++) {
if (*c == ',') point_count++;
}
// Allocate points array
chords[i].points = malloc(point_count * sizeof(uint32_t));
chords[i].point_count = point_count;
// Parse points
char* point_start = line;
int point_idx = 0;
while (*point_start && *point_start != '\n') {
char* end;
uint32_t point = strtoul(point_start, &end, 10);
if (end == point_start) break;
if (point_idx < point_count) {
chords[i].points[point_idx++] = point;
}
point_start = end;
while (*point_start && (*point_start == ',' || *point_start == ' ')) {
point_start++;
}
}
i++;
}
fclose(fp);
*num_chords_out = num_lines;
return chords;
}
// Write chords with full superpixel data to CSV
static int chords_with_data_to_csv(const char* path,
const Chord* chords,
int num_chords,
const Superpixel* superpixels) {
FILE* fp = fopen(path, "w");
if (!fp) return -1;
// Write header with all fields
fprintf(fp, "chord_id,superpixel_id,z,y,x,intensity,pixel_count\n");
// Write data - each line contains full information about each point in the chord
for (int i = 0; i < num_chords; i++) {
const Chord* chord = &chords[i];
for (int j = 0; j < chord->point_count; j++) {
uint32_t superpixel_id = chord->points[j];
const Superpixel* sp = &superpixels[superpixel_id];
// Write: chord_id, point_index, superpixel_id, superpixel data (z,y,x,intensity,count), position data
fprintf(fp, "%d,%u,%.1f,%.1f,%.1f,%.1f,%u\n",
i, // chord_id
superpixel_id, // original superpixel id
sp->z, // superpixel centroid z
sp->y, // superpixel centroid y
sp->x, // superpixel centroid x
sp->c, // intensity
sp->n // pixel count
);
}
}
fclose(fp);
return 0;
}
// Read chords with full data from CSV
static Chord* csv_to_chords_with_data(const char* path, int* num_chords_out) {
FILE* fp = fopen(path, "r");
if (!fp) return NULL;
char line[1024];
int max_chords = 1024; // Initial capacity
Chord* chords = calloc(max_chords, sizeof(Chord));
if (!chords) {
fclose(fp);
return NULL;
}
// Skip header
fgets(line, sizeof(line), fp);
int current_chord_id = -1;
int num_chords = 0;
Chord* current_chord = NULL;
int point_capacity = 0;
// Read data line by line
while (fgets(line, sizeof(line), fp)) {
int chord_id;
uint32_t superpixel_id;
float sp_z, sp_y, sp_x, intensity;
unsigned int pixel_count;
if (sscanf(line, "%d,%u,%f,%f,%f,%f,%u",
&chord_id, &superpixel_id,
&sp_z, &sp_y, &sp_x, &intensity, &pixel_count) != 8) {
continue;
}
// If this is a new chord
if (chord_id != current_chord_id) {
current_chord_id = chord_id;
// Expand chords array if needed
if (chord_id >= max_chords) {
int new_max = max_chords * 2;
Chord* new_chords = realloc(chords, new_max * sizeof(Chord));
if (!new_chords) {
for (int i = 0; i < num_chords; i++) {
free(chords[i].points);
free(chords[i].recent_dirs);
}
free(chords);
fclose(fp);
return NULL;
}
chords = new_chords;
max_chords = new_max;
}
current_chord = &chords[chord_id];
point_capacity = 128; // Initial point capacity
current_chord->points = malloc(point_capacity * sizeof(uint32_t));
current_chord->recent_dirs = malloc(MAX_RECENT_DIRS * NUM_DIMENSIONS * sizeof(float));
current_chord->point_count = 0;
current_chord->num_recent_dirs = 0;
if (chord_id >= num_chords) {
num_chords = chord_id + 1;
}
}
}
fclose(fp);
*num_chords_out = num_chords;
return chords;
}
static bool file_exists(char* path) {
struct stat path_stat;
return stat(path, &path_stat) == 0;
}