aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 5e782ee155ff56b641856d5c292e7f74a1032a6b (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#ifdef VISUAL
#include <raylib.h>
#endif

#include <lib/lib.h>
#include <lib/png.h>
#include <lib/bool.h>
#include <lib/monad.h>
#include <lib/dir.h>
#include <lib/file.h>
#include <lib/time.h>
#include <lib/color.h>
#include <lib/algo/flood_fill.h>
#include <lib/algo/avl_tree.h>
#include <lib/seg/util.h>
#include <lib/seg/mask_data.h>

#define OFFSET 16

#define N_DILATIONS 10

#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;
  //-----------------------------------------------
  //-GET-COMMAND-LINE-ARGUMENTS--------------------
  //-----------------------------------------------
  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;
  //-----------------------------------------------
  //-PROCESS-FILES-IN-DIRECTORY--------------------
  //-----------------------------------------------
  char** file_list = NULL;
  uint32_t width, height;
  uint16_t starting_label = 1;
  uint16_t *masks = NULL;
  // Expect a directory to be passed as the first argument
  if (directory != NULL) {
    // Ensure the directory exists
    if (is_directory(directory)) {
      // List files in the ddirectory
      file_list = list_directory(directory);
      if (file_list != NULL) {
	for (size_t index = 0; file_list[index] != NULL; index++) {
	  char* fname = file_list[index];
	  if (is_tif_ext(fname) == FALSE) {
	    free(file_list[index]);
	    continue;
	  }
	  // If we have a tiff file
	  //  1. Convert to labels
	  //  2. Find contiguous regions
	  //  3. Combine with current total mask
	  //  4. Free up allocations made in this process
	  char* fpath = full_path(directory, fname);
	  if (fpath == NULL) {
	    free(file_list[index]);
	    continue;
	  }
	  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);
	    free(file_list[index]);
	    continue;
	  }
	  masks = combine_masks(masks, file_labels, width, height);
	  free(file_labels);
	  free(fpath);
	  free(file_list[index]);
	}
	free(file_list);
      }
    }
  }
  if (masks == NULL) {
    fprintf(stderr, "No masks found!\n");
    return 1;
  }

  //-----------------------------------------------
  //-FIND-CONTIGUOUS-REGIONS-----------------------
  //-----------------------------------------------
  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_filter_start);
  //-----------------------------------------------
  //-FILTER-SMALL-REGIONS-OUT----------------------
  //-----------------------------------------------
  filter_small_masks(masks, width, height, MIN_AREA, MIN_PERIMETER);
  TIME(ts_filter_end);
  if (!silent) {
    printf("Removing small labels took %f ms\n", 1000*diff_time(&ts_filter_end, &ts_filter_start));
  }
  //-----------------------------------------------
  //-FIND-CONTIGUOUS-REGIONS-----------------------
  //-----------------------------------------------
  //--to-make-labels-span-1-to-n-------------------
  //-----------------------------------------------
  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);
  }
  //-----------------------------------------------
  //-OPTIONAL:-------------------------------------
  //-GET-MASK-META-INFORMATION---------------------
  //-----------------------------------------------
  root = get_mask_data(masks, width, height);
#ifdef AVL_INFO
  if (!silent) {
    printf("Inorder traversal of AVL tree: ");
    print_label(root);
    printf("\n");
  }
#endif
  free_avl_tree_nodes(root);

  TIME(ts_start);
  //-----------------------------------------------
  //-CLOSE-UP-SMALL-GAPS-BETWEEN-REGIONS-----------
  //-----------------------------------------------
  closeup(&masks, width, height, N_DILATIONS);
  TIME(ts_end);
  if (!silent) {
    printf("Closing up took %f ms\n", 1000*diff_time(&ts_end, &ts_start));
  }
  //-----------------------------------------------
  //-END-OF-PROCESSING-----------------------------
  //-----------------------------------------------

#ifdef VISUAL
#include <snippets/raylib_block.c>
#else
  //-----------------------------------------------
  //-SAVE-MASK-AS-BINARY-AND-PNG-------------------
  //-----------------------------------------------
  if (masks != NULL) {
    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);
  }
#endif
  TIME(ts_g_end);
  if (!silent) {
    printf("Finished in %f ms\n", 1000*diff_time(&ts_g_end, &ts_g_start));
  }
  return 0;
}