-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinspect.c
497 lines (438 loc) · 16.9 KB
/
inspect.c
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdbool.h>
#include <sys/stat.h>
#include "utils.h"
#include "parser.h"
#include "spng.h"
static void display_sl1 (sl1_t *sl1);
static void display_ctb (ctb_t *ctb);
static int export_layer (ctb_t *ctb, FILE *ctb_file, FILE *metadata_file, const char *dir, int i);
// TODO
/*
* Display meta data about given sl1 file.
*/
static void
display_sl1 (sl1_t *sl1)
{
printf ("Bed shape: %s\n", sl1->bed_shape);
printf ("Bottle cost: %.2f\n", sl1->bottle_cost);
printf ("Bottle volume: %.2f\n", sl1->bottle_volume);
printf ("Bottle weight: %.2f\n", sl1->bottle_weight);
printf ("Default SLA material profile: %s\n", sl1->default_sla_material_profile);
printf ("Default SLA print profile: %s\n", sl1->default_sla_print_profile);
printf ("Display width: %.2f mm\n", sl1->display_width);
printf ("Display height: %.2f mm\n", sl1->display_height);
printf ("Display pixels x: %d\n", sl1->display_pixels_x);
printf ("Display pixels y: %d\n", sl1->display_pixels_y);
if (sl1->display_mirror_x)
puts ("Display is mirrored on x axis.");
if (sl1->display_mirror_y)
puts ("Display is mirrored on y axis.");
if (sl1->display_orientation == SL1_ORIENTATION_LANDSCAPE)
puts ("Orientation: landscape.");
else
puts ("Orientation: portrait.");
printf ("Exposure time: %.2f secs\n", sl1->exposure_time);
printf ("Faded layer (will be used in ctb file as bottom layer count): %d\n", sl1->faded_layers);
printf ("Fast tilt time: %d secs\n", sl1->fast_tilt_time);
printf ("Initial exposure time: %d secs\n", sl1->initial_exposure_time);
printf ("Initial layer height: %.2f mm\n", sl1->initial_layer_height);
printf ("Layer height: %.2f mm\n", sl1->layer_height);
printf ("Max print height: %.2f mm\n", sl1->max_print_height);
printf ("Printer notes: %s\n", sl1->printer_notes);
printf ("Printer settings id: %s\n", sl1->printer_settings_id);
printf ("SLA material id: %s\n", sl1->sla_material_settings_id);
printf ("SLA print id: %s\n", sl1->sla_print_settings_id);
printf ("Number of fast layers: %d\n", sl1->num_fast_layers);
printf ("Number of slow layers: %d\n", sl1->num_slow_layers);
printf ("Print time: %d secs (%.2f mins, %.2f hrs)\n", sl1->print_time, (double) sl1->print_time / 60, (double) sl1->print_time / 3600);
}
/*
* Display meta data about given ctb file.
*/
static void
display_ctb (ctb_t *ctb)
{
printf ("CTB file, version %d for %s\n", ctb->headers.version, ctb->machine_name);
puts ("\n[Header]");
printf ("Bed size (x, y, z): %.0fx%.0fx%.0fmm\n", ctb->headers.bed_size_x, ctb->headers.bed_size_y, ctb->headers.bed_size_z);
printf ("Height of the printed object: %.2fmm.\n", ctb->headers.total_height);
printf ("Indicative layer height: %.2fmm\n", ctb->headers.layer_height);
printf ("Indicative exposure: %.2fs\n", ctb->headers.layer_exposure);
printf ("Indicative exposure at bottom: %.2fs\n", ctb->headers.bottom_exposure);
printf ("Indicative light off delay: %.2fs\n", ctb->headers.light_off_delay);
printf ("Bottom layer count: %d\n", ctb->headers.bottom_layer_count);
printf ("Resolution (x, y): %dx%d\n", ctb->headers.resolution_x, ctb->headers.resolution_y);
printf ("Layer count: %d\n", ctb->headers.layer_count);
printf ("Estimated print time: %ds.\n", ctb->headers.print_time);
if (ctb->headers.projection)
puts ("Projection is mirrored.");
printf ("Antialias level: %d\n", ctb->headers.antialias_level);
printf ("PWM level: %d\n", ctb->headers.pwm_level);
printf ("Bottom PWM level: %d\n", ctb->headers.bottom_pwm_level);
if (ctb->headers.encryption_key)
printf ("File is encrypted! Key: %d\n", ctb->headers.encryption_key);
printf ("Layer table offset: %d\n", ctb->headers.layer_table_offset);
printf ("Large preview offset: %d\n", ctb->headers.large_preview_offset);
printf ("Small preview offset: %d\n", ctb->headers.small_preview_offset);
printf ("Print config offset: %d\n", ctb->headers.print_config_offset);
printf ("Slicer config offset: %d\n", ctb->headers.slicer_config_offset);
puts ("\n[Print config]");
printf ("Bottom lift height: %.2fmm.\n", ctb->print_config.bottom_lift_height);
printf ("Bottom lift speed: %.2fmm/min.\n", ctb->print_config.bottom_lift_speed);
printf ("Lift height: %.2fmm.\n", ctb->print_config.lift_height);
printf ("Lift speed: %.2fmm/min.\n", ctb->print_config.lift_speed);
printf ("Retract speed: %.2fmm/min.\n", ctb->print_config.retract_speed);
printf ("Resin needed: %.2fml, %.2fg.\n", ctb->print_config.resin_volume, ctb->print_config.resin_mass);
printf ("Resin cost: %.2f€.\n", ctb->print_config.resin_cost);
puts ("\n[Slicer config]");
printf ("Bottom lift height 2: %.2fmm.\n", ctb->slicer_config.bottom_lift_height2);
printf ("Bottom lift speed 2: %.2fmm/sec.\n", ctb->slicer_config.bottom_lift_speed2);
printf ("Lift height 2: %.2fmm.\n", ctb->slicer_config.lift_height2);
printf ("Lift distance speed 2: %.2fmm/sec.\n", ctb->slicer_config.lift_speed2);
printf ("Retract distance 2: %.2fmm.\n", ctb->slicer_config.retract_distance2);
printf ("Retract speed 2: %.2fmm/sec.\n", ctb->slicer_config.retract_speed2);
printf ("Reset time after lift: %.2fsec.\n", ctb->slicer_config.reset_time_after_lift);
switch (ctb->slicer_config.per_layer_settings)
{
case 0xF:
puts ("Per layer settings are disabled.");
break;
case 0x2000000F:
puts ("Per layer settings are enabled (v3).");
break;
case 0x4000000F:
puts ("Per layer settings are enabled (v4).");
break;
}
printf ("Mysterious id is %d.\n", ctb->slicer_config.mysterious_id);
printf ("Antialias level: %d\n", ctb->slicer_config.antialias_level);
printf ("Software version: %X\n", ctb->slicer_config.software_version);
printf ("Rest time after retract: %.2f\n", ctb->slicer_config.rest_time_after_retract);
printf ("Rest time after lift 2: %.2f\n", ctb->slicer_config.rest_time_after_lift2);
printf ("Transition layer count : %d\n", ctb->slicer_config.transition_layer_count);
printf ("Machine type offset: %d\n", ctb->slicer_config.machine_type_offset);
printf ("Print config v4 offset: %d\n", ctb->slicer_config.print_config_v4_offset);
if (ctb->headers.version == 4)
{
puts ("\n[Print config v4]");
printf ("Bottom retract speed: %.2f\n", ctb->print_config_v4.bottom_retract_speed);
printf ("Bottom retract speed 2: %.2f\n", ctb->print_config_v4.bottom_retract_speed2);
printf ("Rest time after retract : %.2f\n", ctb->print_config_v4.rest_time_after_retract);
printf ("Rest time after lift : %.2f\n", ctb->print_config_v4.rest_time_after_lift);
printf ("Rest time before lift : %.2f\n", ctb->print_config_v4.rest_time_before_lift);
printf ("Bottom retract height 2 : %.2f\n", ctb->print_config_v4.bottom_retract_height2);
printf ("Disclaimer offset : %d\n", ctb->print_config_v4.disclaimer_offset);
printf ("Disclaimer : %s\n", ctb->v4_disclaimer);
}
puts ("\n[Large preview]");
printf ("Resolution: %dx%d\n", ctb->large_preview.resolution_x, ctb->large_preview.resolution_y);
printf ("Image offset : %d\n", ctb->large_preview.image_offset);
puts ("\n[Small preview]");
printf ("Resolution: %dx%d\n", ctb->small_preview.resolution_x, ctb->small_preview.resolution_y);
printf ("Image offset : %d\n", ctb->small_preview.image_offset);
}
/*
* Extract next image in `file` to the directory `dir`,
* naming it with `i`.
*
* `file` must be already seek to the proper entry in the
* layer headers table.
*
* Returns non-zero in case of error.
*/
static int
export_layer (ctb_t *ctb, FILE *ctb_file, FILE *metadata_file, const char *dir, int layer_index)
{
int err = 0;
uint8_t *raw_data = NULL;
uint8_t *data = NULL;
spng_ctx *ctx = NULL;
FILE *file = NULL;
ctb_layer_header_t *hdr = &ctb->layer_headers[layer_index];
err = fseek (ctb_file, hdr->base.data_offset, SEEK_SET);
if (err)
{
fprintf (stderr, "inspect.c: export_layer() : can't seek ctb file.\n");
goto cleanup;
}
raw_data = xalloc (hdr->base.data_len);
size_t count = fread (raw_data, hdr->base.data_len, 1, ctb_file);
if (count != 1)
{
err = 1;
fprintf (stderr, "inspect.c: export_layer() : can't read layer raw data.\n");
goto cleanup;
}
if (ctb->headers.encryption_key)
decrypt_layer (raw_data, ctb->headers.encryption_key, layer_index, hdr->base.data_len);
size_t data_len = 0;
size_t nonzero_pixels_count = 0;
err = decode_layer (&data, &data_len, raw_data, hdr->base.data_len, ctb, &nonzero_pixels_count);
if (err)
{
fprintf (stderr, "inspect.c: export_layer() : can't decode layer data.\n");
goto cleanup;
}
fprintf (metadata_file, "[Layer %d]\n", layer_index);
fprintf (metadata_file, "Non-zero pixels: %ld\n", nonzero_pixels_count);
fprintf (metadata_file, "BASE\n");
fprintf (metadata_file, "Z: %.3f\n", hdr->base.z);
fprintf (metadata_file, "Exposure: %.3f secs\n", hdr->base.exposure_time);
fprintf (metadata_file, "Light Off Time: %.3f secs\n", hdr->base.light_off_delay);
fprintf (metadata_file, "Data offset: %u\n", hdr->base.data_offset);
fprintf (metadata_file, "Data len: %u\n", hdr->base.data_len);
fprintf (metadata_file, "Unknown1: %u\n", hdr->base.unknown1);
fprintf (metadata_file, "Table size: %u\n", hdr->base.table_size);
fprintf (metadata_file, "Unknown2: %u\n", hdr->base.unknown2);
fprintf (metadata_file, "Unknown3: %u\n", hdr->base.unknown3);
fprintf (metadata_file, "EXTENDED\n");
fprintf (metadata_file, "Z: %.3f\n", hdr->extended.z);
fprintf (metadata_file, "Exposure: %.3f secs\n", hdr->extended.exposure_time);
fprintf (metadata_file, "Light Off Time: %.3f secs\n", hdr->extended.light_off_delay);
fprintf (metadata_file, "Data offset: %u\n", hdr->extended.data_offset);
fprintf (metadata_file, "Data len: %u\n", hdr->extended.data_len);
fprintf (metadata_file, "Unknown1: %u\n", hdr->extended.unknown1);
fprintf (metadata_file, "Table size: %u\n", hdr->extended.table_size);
fprintf (metadata_file, "Unknown2: %u\n", hdr->extended.unknown2);
fprintf (metadata_file, "Unknown3: %u\n", hdr->extended.unknown3);
fprintf (metadata_file, "Lift height: %.3f mm\n", hdr->extended.lift_height);
fprintf (metadata_file, "Lift speed: %.3f mm/s\n", hdr->extended.lift_speed);
fprintf (metadata_file, "Lift height2: %.3f mm\n", hdr->extended.lift_height2);
fprintf (metadata_file, "Lift speed2: %.3f mm/s\n", hdr->extended.lift_speed2);
fprintf (metadata_file, "Retract speed: %.3f mm/s\n", hdr->extended.retract_speed);
fprintf (metadata_file, "Retract distance2: %.3f mm\n", hdr->extended.retract_height2);
fprintf (metadata_file, "Retract speed2: %.3f mm/s\n", hdr->extended.retract_speed2);
fprintf (metadata_file, "Rest time before lift: %.3f secs\n", hdr->extended.rest_time_before_lift);
fprintf (metadata_file, "Rest time after lift: %.3f secs\n", hdr->extended.rest_time_after_lift);
fprintf (metadata_file, "Rest time after retract: %.3f secs\n", hdr->extended.rest_time_after_retract);
fprintf (metadata_file, "Light PWM: %.2f secs\n", hdr->extended.light_pwm);
fputs ("\n", metadata_file);
char filename[250] = "";
snprintf (filename, 249, "%s/%04d.png", dir, layer_index);
file = fopen (filename, "wb");
if (!file)
{
err = 1;
fprintf (stderr, "inspect.c: can't open file for writing: %s\n", filename);
goto cleanup;
}
ctx = spng_ctx_new (SPNG_CTX_ENCODER);
err = spng_set_png_file (ctx, file);
if (err)
{
fprintf (stderr, "inspect.c: export_layer() : Error while passing file to spng.\n");
err = 1;
goto cleanup;
}
struct spng_ihdr headers = {
.width = ctb->headers.resolution_x,
.height = ctb->headers.resolution_y,
.bit_depth = 8,
.color_type = SPNG_COLOR_TYPE_GRAYSCALE,
.compression_method = 0,
.filter_method = SPNG_FILTER_NONE,
.interlace_method = SPNG_INTERLACE_NONE,
};
err = spng_set_ihdr (ctx, &headers);
if (err)
{
fprintf (stderr, "inspect.c: export_layer() : Can't set headers.\n");
goto cleanup;
}
err = spng_encode_image (ctx, data, data_len, SPNG_FMT_PNG, SPNG_ENCODE_FINALIZE);
if (err)
{
fprintf (stderr, "inspect.c: export_layer() : Can't encode image: %s.\n", spng_strerror (err));
goto cleanup;
}
cleanup:
if (raw_data) free (raw_data);
if (data) free (data);
if (file) fclose (file);
if (ctx) spng_ctx_free (ctx);
return err;
}
// PUBLIC
/*
* Extract large preview image and display it.
*
* `image_type` is PREVIEW_LARGE or PREVIEW_SMALL.
*
* Returns non-zero in case of error.
*/
int
show_preview_image (const char *filename, int image_type, const char *dir)
{
int err = 0;
uint8_t *data = NULL;
FILE *file = NULL;
size_t len = 0;
spng_ctx *ctx = NULL;
char out_name[1000] = {0};
snprintf (out_name, 999, "%s/preview.png", dir);
ctb_t *ctb = new_ctb ();
err = parse_ctb_file (ctb, filename);
if (err)
{
fprintf (stderr, "inspect.c: show_preview_image() : Unrecognized file at %s\n", filename);
goto cleanup;
}
err = ctb_read_preview_file (&data, &len, ctb, image_type);
if (err)
{
fprintf (stderr, "inspect.c: show_preview_image() : Can't read image data\n");
goto cleanup;
}
file = fopen (out_name, "wb");
if (!file)
{
fprintf (stderr, "inspect.c: show_preview_image() : Can't open destination file.\n");
err = 1;
goto cleanup;
}
ctb_preview_t *preview = image_type == PREVIEW_LARGE ? &ctb->large_preview : &ctb->small_preview;
uint32_t width = preview->resolution_x;
uint32_t height = preview->resolution_y;
ctx = spng_ctx_new (SPNG_CTX_ENCODER);
err = spng_set_png_file (ctx, file);
if (err)
{
fprintf (stderr, "inspect.c: show_preview_image() : Error while passing file to spng.\n");
err = 1;
goto cleanup;
}
struct spng_ihdr headers = {
.width = width,
.height = height,
.bit_depth = 8,
.color_type = SPNG_COLOR_TYPE_TRUECOLOR,
.compression_method = 0,
.filter_method = SPNG_FILTER_NONE,
.interlace_method = SPNG_INTERLACE_NONE,
};
err = spng_set_ihdr (ctx, &headers);
if (err)
{
fprintf (stderr, "inspect.c: show_preview_image() : Can't set headers.\n");
goto cleanup;
}
err = spng_encode_image (ctx, data, len, SPNG_FMT_PNG, SPNG_ENCODE_FINALIZE);
if (err)
{
fprintf (stderr, "inspect.c: show_preview_image() : Can't encode image: %s.\n", spng_strerror (err));
goto cleanup;
}
fclose (file);
file = NULL;
cleanup:
if (ctb) free_ctb (ctb);
if (data) free (data);
if (file) fclose (file);
if (ctx) spng_ctx_free (ctx);
return err;
}
/*
* Inspect given file, showing its type and most relevant
* information.
*/
int
inspect (const char *in)
{
int ret;
sl1_t *sl1 = NULL;
ctb_t *ctb = NULL;
sl1 = new_sl1 ();
int err = parse_sl1_archive (sl1, in);
if (!err)
{
display_sl1 (sl1);
ret = 0;
goto cleanup;
}
ctb = new_ctb ();
err = parse_ctb_file (ctb, in);
if (err)
{
fprintf (stderr, "inspect() : Unrecognized file at %s\n", in);
ret = 1;
goto cleanup;
}
else
{
display_ctb (ctb);
ret = 0;
goto cleanup;
}
cleanup:
if (sl1) free_sl1 (sl1);
if (ctb) free_ctb (ctb);
return ret;
}
/*
* Export each layer image from ctb file at `filename` to the directory at
* `dir`.
*
* Returns non-zero in case of error.
*/
int
export_layers (const char *filename, const char *dir)
{
int err = 0;
ctb_t *ctb = NULL;
FILE *ctb_file = NULL;
FILE *metadata_file = NULL;
char metadata_filename[750] = "";
struct stat s;
err = stat (dir, &s);
if (err)
{
fprintf (stderr, "export_layers() : directory %s does not exists.\n", dir);
goto cleanup;
}
if (!S_ISDIR (s.st_mode))
{
fprintf (stderr, "export_layers() : %s is not a directory.\n", dir);
err = 1;
goto cleanup;
}
snprintf (metadata_filename, 749, "%s/metadata.txt", dir);
metadata_file = fopen (metadata_filename, "w");
if (!metadata_file)
{
fprintf (stderr, "export_layers() : can't open file %s for writing.\n", metadata_filename);
err = 1;
goto cleanup;
}
ctb_file = fopen (filename, "rb");
if (!ctb_file)
{
fprintf (stderr, "export_layers() : can't read file %s.\n", filename);
err = 1;
goto cleanup;
}
ctb = new_ctb ();
err = parse_ctb_file (ctb, filename);
if (err)
{
fprintf (stderr, "export_layers() : Can't parse ctb file.\n");
goto cleanup;
}
fseek (ctb_file, ctb->headers.layer_table_offset, SEEK_SET);
for (size_t i = 0; i < ctb->headers.layer_count; i++)
{
err = export_layer (ctb, ctb_file, metadata_file, dir, i);
if (err)
{
fprintf (stderr, "export_layers() : error while processing layer %ld.\n", i);
goto cleanup;
}
}
cleanup:
if (ctb) free_ctb (ctb);
if (ctb_file) fclose (ctb_file);
if (metadata_file) fclose (metadata_file);
return err;
}