aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 8af95d6ba2afa764aefc2c3d42e31d39e6c3b769 (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
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
#include <stdio.h>
#include <stdlib.h>

#include <raylib.h>

#include <lib/lib.h>
#include <lib/bool.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/seg/util.h>

#define OFFSET 16

#define N_DILATIONS 3

struct Result {
  void* data;
  bool_t success;
};

struct MaskData {
  uint16_t label;
  size_t area;
  size_t perimeter;
};

struct MaskData* create_mask_data(uint16_t label) {
  struct MaskData *data = (struct MaskData*)malloc(sizeof(struct MaskData));
  data->label = label;
  data->area = 0;
  data->perimeter = 0;
  return data;
}

struct AVLNode {
  void* data;
  bool_t (*compare)(void*, void*);
  struct AVLNode* left;
  struct AVLNode* right;
  uint8_t height;
};

uint8_t get_height(struct AVLNode* node)
{
  if (node == NULL) {
    return 0;
  }
  return node->height;
}

bool_t compare_labels(struct MaskData* left, struct MaskData* right)
{
  return left->label < right->label;
}

struct AVLNode* create_avl_mask_node(struct MaskData* data)
{
  struct AVLNode* node = (struct AVLNode*)malloc(sizeof(struct AVLNode));
  if (node == NULL) {
    return NULL;
  }
  node->data = data;
  node->compare = (bool_t (*)(void*,void*))&compare_labels;
  node->left = NULL;
  node->right = NULL;
  node->height = 1; // Leaf initially
  return node;
}

uint8_t max_height(uint8_t a, uint8_t b)
{
  return (a > b) ? a : b;
}

ssize_t get_balance_factor(struct AVLNode* node)
{
  if (node == NULL) {
    return 0;
  }
  return get_height(node->left) - get_height(node->right);
}

struct AVLNode* right_rotate(struct AVLNode* parent)
{
  struct AVLNode* child1 = parent->left;
  struct AVLNode* child2 = child1->right;

  child1->right = parent;
  parent->left = child2;

  parent->height = max_height(get_height(parent->left), get_height(parent->right)) + 1;
  child1->height = max_height(get_height(child1->left), get_height(child1->right)) + 1;
  return child1;
}

struct AVLNode* left_rotate(struct AVLNode* parent)
{
  struct AVLNode* child1 = parent->right;
  struct AVLNode* child2 = child1->left;

  child1->left = parent;
  parent->right = child2;

  parent->height = max_height(get_height(parent->left), get_height(parent->right)) + 1;
  child1->height = max_height(get_height(child1->left), get_height(child1->right)) + 1;
  return child1;
}

struct Result insert_mask(struct AVLNode* node, struct MaskData* data)
{
  struct Result result;
  // 1. Standard BST insertion
  if (node == NULL) {
    return (struct Result) {create_avl_mask_node(data), TRUE};
  }

  struct MaskData *node_data = (struct MaskData*)node->data;
  if (node->compare(data, node_data)) {
    result = insert_mask(node->left, data);
    if (!result.success) {
      fprintf(stderr, "Failed to insert!");
      return result;
    }
    node->left = (struct AVLNode*)result.data;
  } else if (node->compare(node->data, data)) {
    result = insert_mask(node->right, data);
    if (!result.success) {
      fprintf(stderr, "Failed to insert!");
      return result;
    }
    node->right = (struct AVLNode*)result.data;
  } else {
    return (struct Result) {node, FALSE};
  }

  // 2. Update height of the ancestor node
  node->height = 1 + max_height(get_height(node->left), get_height(node->right));

  ssize_t balance = get_balance_factor(node);

  // 4. If the node becomes unbalanced

  // LeftLeft
  if ((balance > 1) && node->compare(data, node->left->data)) {
    return (struct Result) {right_rotate(node), TRUE};
  }
  // RightRight
  if ((balance < -1) && node->compare(node->right->data, data)) {
    return (struct Result) {left_rotate(node), TRUE};
  }
  // LeftRight
  if ((balance > 1) && node->compare(node->left->data, data)) {
    return (struct Result) {right_rotate(node), TRUE};
  }
  // RightLeft
  if ((balance < -1) && node->compare(data,node->right->data)) {
    return (struct Result) {left_rotate(node), TRUE};
  }
  return (struct Result) {node, TRUE};
}

struct AVLNode* insert_mask_alloc(struct AVLNode* node, uint16_t label)
{
  struct MaskData* data = create_mask_data(label);
  struct Result result = insert_mask(node, data);
  if (!result.success) {
    free(data);
  }
  return (struct AVLNode*)result.data;
}

void print_label(struct AVLNode* root) {
  if (root != NULL) {
    print_label(root->left);
    struct MaskData* data = root->data;
    printf("%d: (%zu, %zu) ", data->label, data->area, data->perimeter);
    print_label(root->right);
  }
}

void free_avl_tree(struct AVLNode* root) {
  if (root != NULL) {
    free_avl_tree(root->left);
    free_avl_tree(root->right);
    free(root);
  }
}

void free_avl_tree_nodes(struct AVLNode* root) {
  if (root != NULL) {
    free_avl_tree_nodes(root->left);
    free_avl_tree_nodes(root->right);
    if (root->data != NULL) {
      free(root->data);
    }
    free(root);
  }
}

bool_t increase_label_area(struct AVLNode* root, uint16_t label) {
  if (root == NULL) {
    return FALSE;
  }
  struct MaskData* data = (struct MaskData*)root->data;
  if (data->label == label) {
    data->area++;
  }
  else if (data->label > label) {
    increase_label_area(root->left, label);
  }
  else if (data->label < label) {
    increase_label_area(root->right, label);
  }
  return TRUE;
}

bool_t increase_label_perimeter(struct AVLNode* root, uint16_t label) {
  if (root == NULL) {
    return FALSE;
  }
  struct MaskData* data = (struct MaskData*)root->data;
  if (data->label == label) {
    data->perimeter++;
  }
  else if (data->label > label) {
    increase_label_perimeter(root->left, label);
  }
  else if (data->label < label) {
    increase_label_perimeter(root->right, label);
  }
  return TRUE;
}

int main(int argc, char** argv)
{
  struct AVLNode* root = NULL;
  root = insert_mask_alloc(root, 2);
  root = insert_mask_alloc(root, 5);
  root = insert_mask_alloc(root, 1);
  root = insert_mask_alloc(root, 3);
  root = insert_mask_alloc(root, 4);
  printf("Inorder traversal of AVL tree: ");
  print_label(root);
  printf("\n");
  free_avl_tree_nodes(root);
  //-----------------------------------------------
  //-LIST-FILES-IN-DIRECTORY-----------------------
  //-----------------------------------------------
  char** file_list = NULL;
  uint32_t width, height;
  uint16_t starting_label = 1;
  uint16_t *masks = NULL;
  if (argc > 1) {
    if (is_directory(argv[1])) {
      file_list = list_directory(argv[1]);
      if (file_list) {
	size_t index = 0;
	while (1) {
	  char* fname = file_list[index];
	  if (fname == NULL) {
	    break;
	  }
	  if (is_tif_ext(fname) == FALSE) {
	    free(file_list[index++]);
	    continue;
	  }
	  char* fpath = full_path(argv[1], fname);
	  printf("Loading %s...\n", fpath);
	  uint16_t *file_labels = tif_to_labels(fpath, &width, &height, &starting_label);
	  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;
  }
  printf("%u labels found\n", starting_label-1);
  printf("Mask dimensions: %u %u\n", width, height);
  TIME(ts_start);
  for (uint16_t count = 0; count < N_DILATIONS; count++) {
    uint16_t *new_labels = dilate(masks, width, height);
    free(masks);
    masks = new_labels;
  }
  TIME(ts_end);
  printf("Dilation took %f ms\n", 1000*diff_time(&ts_end, &ts_start));
  //free(masks);
  //-----------------------------------------------

  //-----------------------------------------------
  //-RAYLIB-INIT
  //-----------------------------------------------
  SetTraceLogLevel(LOG_ERROR);
  SetConfigFlags(FLAG_WINDOW_RESIZABLE);
  const char* gui_title = "Image Manip - Useful for segmentations!";
  InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, gui_title);

  //-----------------------------------------------
  // (When treating this as RGBA, last bits should ensure opaque)
  //  This assumes 4096 (2^12) > labels
  for (size_t y = 0; y < height; y++) {
    for (size_t x = 0; x < width; x++) {
      /// RGBA channels: Move labels to RGB
      masks[x + y*width] <<= 4;
      masks[x + y*width] |= 0x000F;
    }
  }
  //-----------------------------------------------

  //-----------------------------------------------
  //-RAYLIB-IMAGE-STRUCTURING----------------------
  //-----------------------------------------------
  Image RaylibImage;
  RaylibImage.width  = width;
  RaylibImage.height = height;
  RaylibImage.mipmaps = 1;
  // Use Contiguous Labels
  RaylibImage.data = masks;
  RaylibImage.format = PIXELFORMAT_UNCOMPRESSED_R4G4B4A4;
  //-----------------------------------------------

  // Image to a Texture
  Texture2D RaylibTexture = LoadTextureFromImage(RaylibImage);

  // Scale the image to the viewport
  /// Source Rectangle: Original Size
  Rectangle sourceRec = { 0.0f, 0.0f, (float)width, (float)height };
  /// Destination Rectangle: Transformed Size
  Rectangle destRec = { 0.0f, 0.0f, (float)SCREEN_WIDTH, (float)(SCREEN_HEIGHT-OFFSET) };
  /// Location to begin drawing
  Vector2 origin = { (float)0, (float)-OFFSET };

  // Raylib boilerplate
  SetTargetFPS(60);
  Camera2D camera = { 0 };
  camera.zoom = 1.0f;

  // GUI Loop
  while (!WindowShouldClose()) {
    //-----------------------------------------------
    //-DRAWING---------------------------------------
    //-----------------------------------------------
    BeginDrawing();
    ClearBackground(RAYWHITE);
    BeginMode2D(camera);
    EndMode2D();
    DrawText("Image Manip", 0, 0, OFFSET, DARKGRAY);
    DrawTexturePro(RaylibTexture, sourceRec, destRec, origin, (float)0,
                   RAYWHITE);
    /*
    uint32_t x = 0x49, y = 0x4A;
    uint32_t dx = 0x69 - x, dy = 0x6E - y;
    x = (SCREEN_WIDTH*x)/width;
    y = SCREEN_HEIGHT-((SCREEN_HEIGHT-OFFSET)*y)/height;
    dx = (SCREEN_WIDTH*dx)/width;
    dy = SCREEN_HEIGHT-((SCREEN_HEIGHT-OFFSET)*dy)/height;
    DrawRectangleGradientH(x, y, dx, dy, BLUE, PURPLE);
    */
    EndDrawing();
    //-----------------------------------------------
  }

  if (masks != NULL) {
    for (size_t y = 0; y < height; y++) {
      for (size_t x = 0; x < width; x++) {
	/// Restore labels from RGBA
	masks[x + y*width] &= 0xFFF0;
	masks[x + y*width] >>= 4;
      }
    }
    write_array("../out.bin", masks, width*height*sizeof(uint16_t));
    free(masks);
  }
  CloseWindow();
  return 0;
}