aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c205
1 files changed, 89 insertions, 116 deletions
diff --git a/src/main.c b/src/main.c
index 5bdcadb..a18d602 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h>
#ifdef VISUAL
#include <raylib.h>
@@ -22,11 +23,56 @@
#define N_DILATIONS 10
-#define MIN_AREA 200
-#define MIN_PERIMETER 50
+#define MIN_AREA 500
+#define MIN_PERIMETER 0
int main(int argc, char** argv)
{
+ char opt;
+ char* directory = NULL;
+ char* png_file = "../out.png";
+ char* bin_file = "../out.bin";
+ bool_t silent = FALSE;
+ while ((opt = getopt(argc, argv, "d:b:p:s")) != -1) {
+ switch (opt) {
+ case 's':
+ silent = TRUE;
+ break;
+ case 'd':
+ if (!silent) {
+ printf("Parse Directory: %s\n", optarg);
+ }
+ directory = optarg;
+ break;
+ case 'b':
+ if (!silent) {
+ printf("Bin File: %s\n", optarg);
+ }
+ bin_file = optarg;
+ break;
+ case 'p':
+ if (!silent) {
+ printf("PNG File: %s\n", optarg);
+ }
+ png_file = optarg;
+ break;
+ case ':':
+ if (!silent) {
+ printf("Option requires value\n");
+ }
+ break;
+ case '?':
+ if (!silent) {
+ printf("Unknown option: %c\n", optopt);
+ }
+ break;
+ }
+ }
+ if (!silent) {
+ for (;optind < argc; optind++) {
+ printf("Extra arguments: %s\n", argv[optind]);
+ }
+ }
TIME(ts_g_start);
struct AVLNode* root = NULL;
//-----------------------------------------------
@@ -37,11 +83,11 @@ int main(int argc, char** argv)
uint16_t starting_label = 1;
uint16_t *masks = NULL;
// Expect a directory to be passed as the first argument
- if (argc > 1) {
+ if (directory != NULL) {
// Ensure the directory exists
- if (is_directory(argv[1])) {
+ if (is_directory(directory)) {
// List files in the ddirectory
- file_list = list_directory(argv[1]);
+ file_list = list_directory(directory);
if (file_list) {
size_t index = 0;
// For each file
@@ -59,12 +105,14 @@ int main(int argc, char** argv)
// 2. Find contiguous regions
// 3. Combine with current total mask
// 4. Free up allocations made in this process
- char* fpath = full_path(argv[1], fname);
+ char* fpath = full_path(directory, fname);
if (fpath == NULL) {
free(file_list[index++]);
continue;
}
- printf("Loading %s...\n", fpath);
+ if (!silent) {
+ printf("Loading %s...\n", fpath);
+ }
uint16_t *file_labels = tif_to_labels(fpath, &width, &height, &starting_label);
if (file_labels == NULL) {
free(fpath);
@@ -85,116 +133,44 @@ int main(int argc, char** argv)
return 1;
}
+ uint16_t* new_masks;
// Regenerate contiguous labels
- starting_label = 1;
- uint16_t* labels = (uint16_t*)calloc(width*height, sizeof(uint16_t));
- if (labels == NULL) {
- fprintf(stderr, "Memory allocation error\n");
- free(masks);
- }
- for (size_t y = 0; y < height; y++) {
- for (size_t x = 0; x < width; x++) {
- size_t coord = x + y*width;
- uint8_t channels = 2;
- if (flood((uint8_t*)masks, labels, width, height, channels, x, y, &(((uint8_t*)masks)[coord*channels]), starting_label)) {
- starting_label++;
- }
- }
- }
- uint16_t *temp = labels;
- labels = masks;
- masks = temp;
- free(labels);
- printf("%u labels found\n", starting_label-1);
- printf("Mask dimensions: %u %u\n", width, height);
- // Get the area/ perimeter of each label
- root = NULL;
- TIME(ts_info_start);
- for (size_t y = 0; y < height; y++) {
- for (size_t x = 0; x < width; x++) {
- if (masks[x + y*width] != 0) {
- root = increase_label_area_alloc(root, masks[x + y*width]);
- if (is_on_mask_boundary(masks, width, height, x, y)) {
- increase_label_perimeter(root, masks[x + y*width]);
- }
- }
- }
+ reduce_contiguous_regions(&masks, width, height, &starting_label);
+ if (!silent) {
+ printf("%u labels found\n", starting_label-1);
+ printf("Mask dimensions: %u %u\n", width, height);
}
- TIME(ts_info_end);
- printf("Information retrieval took %f ms\n", 1000*diff_time(&ts_info_end, &ts_info_start));
-#ifdef AVL_INFO
- printf("Inorder traversal of AVL tree: ");
- print_label(root);
- printf("\n");
-#endif
TIME(ts_filter_start);
- // Get the smallest labels
- struct AVLNode* small_label_tree = NULL;
- small_label_tree = get_small_labels(NULL, root, MIN_AREA, MIN_PERIMETER);
-
- // Remove the small labels
- for (size_t y = 0; y < height; y++) {
- for (size_t x = 0; x < width; x++) {
- if (in_uint16_t_tree(small_label_tree, masks[x + y*width])) {
- masks[x + y*width] = 0;
- }
- }
- }
+ filter_small_masks(masks, width, height, MIN_AREA, MIN_PERIMETER);
TIME(ts_filter_end);
- printf("Removing small labels took %f ms\n", 1000*diff_time(&ts_filter_end, &ts_filter_start));
- free_avl_tree(small_label_tree);
- free_avl_tree_nodes(root);
+ if (!silent) {
+ printf("Removing small labels took %f ms\n", 1000*diff_time(&ts_filter_end, &ts_filter_start));
+ }
// Regenerate contiguous labels
- starting_label = 1;
- labels = (uint16_t*)calloc(width*height, sizeof(uint16_t));
- if (labels == NULL) {
- fprintf(stderr, "Memory allocation error\n");
- free(masks);
- }
- for (size_t y = 0; y < height; y++) {
- for (size_t x = 0; x < width; x++) {
- size_t coord = x + y*width;
- uint8_t channels = 2;
- if (flood((uint8_t*)masks, labels, width, height, channels, x, y, &(((uint8_t*)masks)[coord*channels]), starting_label)) {
- starting_label++;
- }
- }
+ reduce_contiguous_regions(&masks, width, height, &starting_label);
+ if (!silent) {
+ printf("%u remaining labels found\n", starting_label-1);
+ printf("Mask dimensions: %u %u\n", width, height);
}
- temp = labels;
- labels = masks;
- masks = temp;
- free(labels);
- printf("%u remaining labels found\n", starting_label-1);
- printf("Mask dimensions: %u %u\n", width, height);
// Regenerate information after relabeling
- root = NULL;
- for (size_t y = 0; y < height; y++) {
- for (size_t x = 0; x < width; x++) {
- if (masks[x + y*width] != 0) {
- root = increase_label_area_alloc(root, masks[x + y*width]);
- if (is_on_mask_boundary(masks, width, height, x, y)) {
- increase_label_perimeter(root, masks[x + y*width]);
- }
- }
- }
- }
+ root = get_mask_data(masks, width, height);
#ifdef AVL_INFO
- printf("Inorder traversal of AVL tree: ");
- print_label(root);
- printf("\n");
+ if (!silent) {
+ printf("Inorder traversal of AVL tree: ");
+ print_label(root);
+ printf("\n");
+ }
#endif
free_avl_tree_nodes(root);
uint16_t *new_labels;
TIME(ts_start);
- new_labels = closeup(masks, width, height, N_DILATIONS);
- if (new_labels != NULL) {
- free(masks);
- masks = new_labels;
- }
+ closeup(&masks, width, height, N_DILATIONS);
TIME(ts_end);
- printf("Closing up took %f ms\n", 1000*diff_time(&ts_end, &ts_start));
+ if (!silent) {
+ printf("Closing up took %f ms\n", 1000*diff_time(&ts_end, &ts_start));
+ }
//-----------------------------------------------
#ifdef VISUAL
@@ -279,32 +255,29 @@ int main(int argc, char** argv)
masks[x + y*width] >>= 4;
}
}
- write_array("../out.bin", masks, width*height*sizeof(uint16_t));
+ struct bitmap_t* bitmap = uint16_to_bitmap(masks, width, height);
+ if (bitmap != NULL) {
+ save_png(bitmap, png_file);
+ free(bitmap);
+ }
+ write_array(bin_file, masks, width*height*sizeof(uint16_t));
free(masks);
}
CloseWindow();
#else
if (masks != NULL) {
struct bitmap_t* bitmap = uint16_to_bitmap(masks, width, height);
- char* png_name = "../out.png";
- char* bin_name = "../out.bin";
if (bitmap != NULL) {
- if (argc > 3) {
- save_png(bitmap, argv[3]);
- } else {
- save_png(bitmap, png_name);
- }
+ save_png(bitmap, png_file);
free(bitmap);
}
- if (argc > 2) {
- write_array(argv[2], masks, width*height*sizeof(uint16_t));
- } else {
- write_array(bin_name, masks, width*height*sizeof(uint16_t));
- }
+ write_array(bin_file, masks, width*height*sizeof(uint16_t));
free(masks);
}
#endif
TIME(ts_g_end);
- printf("Finished in %f ms\n", 1000*diff_time(&ts_g_end, &ts_g_start));
+ if (!silent) {
+ printf("Finished in %f ms\n", 1000*diff_time(&ts_g_end, &ts_g_start));
+ }
return 0;
}