From a18cea2fef7aa1545c9a984b60919541b26a6f84 Mon Sep 17 00:00:00 2001
From: Christian C <cc@localhost>
Date: Tue, 1 Apr 2025 17:38:32 -0700
Subject: Clang Format

---
 lib/algo/avl_tree.c    |  77 +++++++++-----------
 lib/algo/flood_fill.c  |  23 +++---
 lib/color.c            |  11 +--
 lib/data/image_types.c |  29 ++++----
 lib/dir.c              |  47 ++++++-------
 lib/file.c             |   3 +-
 lib/lib.c              |   2 +-
 lib/png.c              |  27 ++++---
 lib/seg/mask_data.c    | 122 ++++++++++++++------------------
 lib/seg/util.c         | 188 ++++++++++++++++++++++++-------------------------
 lib/time.c             |   8 +--
 11 files changed, 251 insertions(+), 286 deletions(-)

(limited to 'lib')

diff --git a/lib/algo/avl_tree.c b/lib/algo/avl_tree.c
index 7b86bab..cc06254 100644
--- a/lib/algo/avl_tree.c
+++ b/lib/algo/avl_tree.c
@@ -5,8 +5,7 @@
 #include <stdio.h>
 
 // Get the height of an AVL node
-AvlHeight_t get_height(AVLNode* node)
-{
+AvlHeight_t get_height(AVLNode *node) {
   if (node == NULL) {
     return 0;
   }
@@ -14,14 +13,10 @@ AvlHeight_t get_height(AVLNode* node)
 }
 
 // Get the Maximum Height between two
-AvlHeight_t max_height(AvlHeight_t a, AvlHeight_t b)
-{
-  return (a > b) ? a : b;
-}
+AvlHeight_t max_height(AvlHeight_t a, AvlHeight_t b) { return (a > b) ? a : b; }
 
 // Get the balance factor of a node
-ssize_t get_balance_factor(AVLNode* node)
-{
+ssize_t get_balance_factor(AVLNode *node) {
   if (node == NULL) {
     return 0;
   }
@@ -29,37 +24,38 @@ ssize_t get_balance_factor(AVLNode* node)
 }
 
 // Rotate an AVL node right
-AVLNode* right_rotate(AVLNode* parent)
-{
-  AVLNode* child1 = parent->left;
-  AVLNode* child2 = child1->right;
+AVLNode *right_rotate(AVLNode *parent) {
+  AVLNode *child1 = parent->left;
+  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;
+  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;
 }
 
 // Rotate an AVL node left
-AVLNode* left_rotate(AVLNode* parent)
-{
-  AVLNode* child1 = parent->right;
-  AVLNode* child2 = child1->left;
+AVLNode *left_rotate(AVLNode *parent) {
+  AVLNode *child1 = parent->right;
+  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;
+  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;
 }
 
 // Create AVL node
-AVLNode* create_avl_node(void* data, AvlComparator compare)
-{
-  AVLNode* node = (AVLNode*)malloc(sizeof(AVLNode));
+AVLNode *create_avl_node(void *data, AvlComparator compare) {
+  AVLNode *node = (AVLNode *)malloc(sizeof(AVLNode));
   if (node == NULL) {
     return NULL;
   }
@@ -72,12 +68,11 @@ AVLNode* create_avl_node(void* data, AvlComparator compare)
 }
 
 // Insert data into AVL tree
-Result avl_insert(AVLNode* node, void* data, AvlComparator compare)
-{
+Result avl_insert(AVLNode *node, void *data, AvlComparator compare) {
   Result result;
   // 1. Standard BST insertion
   if (node == NULL) {
-    return (Result) {create_avl_node(data, compare), TRUE};
+    return (Result){create_avl_node(data, compare), TRUE};
   }
 
   if (node->compare(data, node->data)) {
@@ -86,20 +81,21 @@ Result avl_insert(AVLNode* node, void* data, AvlComparator compare)
       fprintf(stderr, "Failed to insert!");
       return result;
     }
-    node->left = (AVLNode*)result.data;
+    node->left = (AVLNode *)result.data;
   } else if (node->compare(node->data, data)) {
     result = avl_insert(node->right, data, compare);
     if (!result.success) {
       fprintf(stderr, "Failed to insert!");
       return result;
     }
-    node->right = (AVLNode*)result.data;
+    node->right = (AVLNode *)result.data;
   } else {
-    return (Result) {node, FALSE};
+    return (Result){node, FALSE};
   }
 
   // 2. Update height of the ancestor node
-  node->height = 1 + max_height(get_height(node->left), get_height(node->right));
+  node->height =
+      1 + max_height(get_height(node->left), get_height(node->right));
 
   ssize_t balance = get_balance_factor(node);
 
@@ -107,26 +103,25 @@ Result avl_insert(AVLNode* node, void* data, AvlComparator compare)
 
   // LeftLeft
   if ((balance > 1) && node->compare(data, node->left->data)) {
-    return (Result) {right_rotate(node), TRUE};
+    return (Result){right_rotate(node), TRUE};
   }
   // RightRight
   if ((balance < -1) && node->compare(node->right->data, data)) {
-    return (Result) {left_rotate(node), TRUE};
+    return (Result){left_rotate(node), TRUE};
   }
   // LeftRight
   if ((balance > 1) && node->compare(node->left->data, data)) {
-    return (Result) {right_rotate(node), TRUE};
+    return (Result){right_rotate(node), TRUE};
   }
   // RightLeft
-  if ((balance < -1) && node->compare(data,node->right->data)) {
-    return (Result) {left_rotate(node), TRUE};
+  if ((balance < -1) && node->compare(data, node->right->data)) {
+    return (Result){left_rotate(node), TRUE};
   }
-  return (Result) {node, TRUE};
+  return (Result){node, TRUE};
 }
 
 // In-order traversal print pointer
-void print_in_order(AVLNode* root)
-{
+void print_in_order(AVLNode *root) {
   if (root != NULL) {
     print_in_order(root->left);
     printf("%p ", root->data);
@@ -135,8 +130,7 @@ void print_in_order(AVLNode* root)
 }
 
 // Free avl tree nodes starting at root
-void free_avl_tree(AVLNode* root)
-{
+void free_avl_tree(AVLNode *root) {
   if (root != NULL) {
     free_avl_tree(root->left);
     free_avl_tree(root->right);
@@ -145,8 +139,7 @@ void free_avl_tree(AVLNode* root)
 }
 
 // Free avl tree and their data starting at root
-void free_avl_tree_nodes(AVLNode* root)
-{
+void free_avl_tree_nodes(AVLNode *root) {
   if (root != NULL) {
     free_avl_tree_nodes(root->left);
     free_avl_tree_nodes(root->right);
diff --git a/lib/algo/flood_fill.c b/lib/algo/flood_fill.c
index d11b5aa..e2ee870 100644
--- a/lib/algo/flood_fill.c
+++ b/lib/algo/flood_fill.c
@@ -5,29 +5,30 @@
 //  1. Check that the (x,y) is within the picture
 //  2. Check if the (x,y) coordinate in the mask is unused
 //  3. Check if the (x,y) coordinate in the image is non-background
-//  4. Check if the (x,y) coordinate in the image is the same color as the fill color
+//  4. Check if the (x,y) coordinate in the image is the same color as the fill
+//  color
 //  5. If all hold, set the label for the pixel, and check each neighbor
 //     Otherwise, stop flooding
-bool_t flood(ImageData_t* image, MaskData_t* mask, size_t width, size_t height, size_t channels, size_t x, size_t y, ImageData_t* fill_color, MaskData_t label)
-{
+bool_t flood(ImageData_t *image, MaskData_t *mask, size_t width, size_t height,
+             size_t channels, size_t x, size_t y, ImageData_t *fill_color,
+             MaskData_t label) {
   if ((x >= width) | (y >= height)) {
     return FALSE;
   }
-  size_t coord = x + y*width;
+  size_t coord = x + y * width;
   if (mask[coord] != 0) {
     return FALSE;
   }
-  if (color_zero(&(image[coord*channels]), channels)) {
+  if (color_zero(&(image[coord * channels]), channels)) {
     return FALSE;
   }
-  if (color_equal(&(image[coord*channels]), fill_color, channels)) {
+  if (color_equal(&(image[coord * channels]), fill_color, channels)) {
     mask[coord] = label;
-    flood(image, mask, width, height, channels, x, y+1, fill_color, label);
-    flood(image, mask, width, height, channels, x, y-1, fill_color, label);
-    flood(image, mask, width, height, channels, x+1, y, fill_color, label);
-    flood(image, mask, width, height, channels, x-1, y, fill_color, label);
+    flood(image, mask, width, height, channels, x, y + 1, fill_color, label);
+    flood(image, mask, width, height, channels, x, y - 1, fill_color, label);
+    flood(image, mask, width, height, channels, x + 1, y, fill_color, label);
+    flood(image, mask, width, height, channels, x - 1, y, fill_color, label);
     return TRUE;
   }
   return FALSE;
 }
-
diff --git a/lib/color.c b/lib/color.c
index 172b627..564d7c5 100644
--- a/lib/color.c
+++ b/lib/color.c
@@ -1,10 +1,10 @@
-#include <stdio.h>
-#include <lib/color.h>
 #include <lib/bool.h>
+#include <lib/color.h>
+#include <stdio.h>
 
 // Color Equal to Background
 // Background: zeros in RGB, alpha can be whatever
-bool_t color_zero(const ImageData_t* color1, size_t channels) {
+bool_t color_zero(const ImageData_t *color1, size_t channels) {
   for (size_t idx = 0; idx < channels; idx++) {
     if (idx == 3) {
       break;
@@ -18,7 +18,8 @@ bool_t color_zero(const ImageData_t* color1, size_t channels) {
 
 // Color Equality
 // Determine if two colors are identical
-bool_t color_equal(const ImageData_t* color1, const ImageData_t* color2, size_t channels) {
+bool_t color_equal(const ImageData_t *color1, const ImageData_t *color2,
+                   size_t channels) {
   for (size_t idx = 0; idx < channels; idx++) {
     if (color1[idx] != color2[idx]) {
       return FALSE;
@@ -28,7 +29,7 @@ bool_t color_equal(const ImageData_t* color1, const ImageData_t* color2, size_t
 }
 
 // Print out the `channels` length color vector
-void print_color(ImageData_t* color, size_t channels) {
+void print_color(ImageData_t *color, size_t channels) {
   for (size_t index = 0; index < channels; index++) {
     printf("%hhu ", color[index]);
   }
diff --git a/lib/data/image_types.c b/lib/data/image_types.c
index 0e40ad1..9a0e0ce 100644
--- a/lib/data/image_types.c
+++ b/lib/data/image_types.c
@@ -1,10 +1,9 @@
 #include <lib/data/image_types.h>
-#include <stdlib.h>
 #include <stdio.h>
+#include <stdlib.h>
 
-Image* create_image(size_t width, size_t height, size_t channels)
-{
-  Image* ip = (Image*)malloc(sizeof(Image));
+Image *create_image(size_t width, size_t height, size_t channels) {
+  Image *ip = (Image *)malloc(sizeof(Image));
   if (ip == NULL) {
     return NULL;
   }
@@ -17,7 +16,8 @@ Image* create_image(size_t width, size_t height, size_t channels)
     free(ip);
     return NULL;
   }
-  ImageData_t *image_data = calloc(width * height*channels, sizeof(ImageData_t));
+  ImageData_t *image_data =
+      calloc(width * height * channels, sizeof(ImageData_t));
   if (image_data == NULL) {
     fprintf(stderr, "Memory allocation error\n");
     free(ip->image);
@@ -36,39 +36,37 @@ Image* create_image(size_t width, size_t height, size_t channels)
       free(ip);
     }
     for (size_t x = 0; x < width; x++) {
-      ip->image[y][x] = &image_data[(y * width + x)*channels];
+      ip->image[y][x] = &image_data[(y * width + x) * channels];
     }
   }
   return ip;
 }
 
-Mask* create_image_mask(size_t width, size_t height)
-{
-  Mask* ip = (Mask*)malloc(sizeof(Mask));
+Mask *create_image_mask(size_t width, size_t height) {
+  Mask *ip = (Mask *)malloc(sizeof(Mask));
   if (ip == NULL) {
     return NULL;
   }
   ip->width = width;
   ip->height = height;
-  ip->image = (MaskData_t**)malloc(sizeof(MaskData_t*) * ip->height);
+  ip->image = (MaskData_t **)malloc(sizeof(MaskData_t *) * ip->height);
   if (ip->image == NULL) {
     free(ip);
     return NULL;
   }
-  MaskData_t* image_data = calloc(width*height, sizeof(MaskData_t));
+  MaskData_t *image_data = calloc(width * height, sizeof(MaskData_t));
   if (image_data == NULL) {
     free(ip->image);
     free(ip);
     return NULL;
   }
   for (size_t y = 0; y < height; y++) {
-    ip->image[y] = &image_data[y*width];
+    ip->image[y] = &image_data[y * width];
   }
   return ip;
 }
 
-void free_image(Image* image)
-{
+void free_image(Image *image) {
   if (image->image[0] != NULL) {
     free(image->image[0]);
     image->image[0] = NULL;
@@ -82,8 +80,7 @@ void free_image(Image* image)
   }
 }
 
-void free_image_mask(Mask* image_mask)
-{
+void free_image_mask(Mask *image_mask) {
   if (image_mask->image[0] != NULL) {
     free(image_mask->image[0]);
     image_mask->image[0] = NULL;
diff --git a/lib/dir.c b/lib/dir.c
index be2a2ac..7200c52 100644
--- a/lib/dir.c
+++ b/lib/dir.c
@@ -1,11 +1,11 @@
-#include <lib/dir.h>
 #include <dirent.h>
-#include <sys/stat.h>
-#include <string.h>
+#include <lib/dir.h>
 #include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
 
 // Is directory
-bool_t is_directory(char* dirname) {
+bool_t is_directory(char *dirname) {
   struct stat st;
   if (stat(dirname, &st) == 0) {
     if (S_ISDIR(st.st_mode)) {
@@ -16,11 +16,11 @@ bool_t is_directory(char* dirname) {
 }
 
 // List directory
-char** list_directory(char* dirname) {
+char **list_directory(char *dirname) {
   DIR *d;
   struct dirent *dir;
   d = opendir(dirname);
-  char** file_names = (char**)malloc(sizeof(char*));
+  char **file_names = (char **)malloc(sizeof(char *));
   if (!file_names) {
     return NULL;
   }
@@ -29,15 +29,15 @@ char** list_directory(char* dirname) {
   if (d) {
     while ((dir = readdir(d)) != NULL) {
       if (dir->d_type == DT_REG) {
-	// When a regular file is reached
-	/// Create space for it in the list
-	file_names = realloc(file_names, (file_count+1+1)*sizeof(char*));
-	/// Create space for the name
-	file_names[file_count] = calloc(strlen(dir->d_name)+1, sizeof(char));
-	/// Copy the name
-	strcpy(file_names[file_count], dir->d_name);
-	/// Mark the end of the file list with a NULL entry
-	file_names[++file_count] = NULL;
+        // When a regular file is reached
+        /// Create space for it in the list
+        file_names = realloc(file_names, (file_count + 1 + 1) * sizeof(char *));
+        /// Create space for the name
+        file_names[file_count] = calloc(strlen(dir->d_name) + 1, sizeof(char));
+        /// Copy the name
+        strcpy(file_names[file_count], dir->d_name);
+        /// Mark the end of the file list with a NULL entry
+        file_names[++file_count] = NULL;
       }
     }
     return file_names;
@@ -46,23 +46,22 @@ char** list_directory(char* dirname) {
 }
 
 // Get full path
-char* full_path(char* dir, char* file)
-{
-  char* fpath = NULL;
+char *full_path(char *dir, char *file) {
+  char *fpath = NULL;
   size_t dir_len = strlen(dir);
   size_t file_len = strlen(file);
-  fpath = (char*)calloc(dir_len+file_len+2,sizeof(char));
-  strcpy(fpath,dir);
-  strcpy(fpath+dir_len+1,file);
+  fpath = (char *)calloc(dir_len + file_len + 2, sizeof(char));
+  strcpy(fpath, dir);
+  strcpy(fpath + dir_len + 1, file);
   fpath[dir_len] = '/';
   return fpath;
 }
 
 // Determines if file name has tif file extension
-bool_t is_tif_ext(char* file_name)
-{
+bool_t is_tif_ext(char *file_name) {
   size_t file_len = strlen(file_name);
-  if ((file_name[file_len-3] == 't') && (file_name[file_len-2] == 'i') && (file_name[file_len-1] == 'f')) {
+  if ((file_name[file_len - 3] == 't') && (file_name[file_len - 2] == 'i') &&
+      (file_name[file_len - 1] == 'f')) {
     return TRUE;
   }
   return FALSE;
diff --git a/lib/file.c b/lib/file.c
index b6ec1d0..6700451 100644
--- a/lib/file.c
+++ b/lib/file.c
@@ -2,8 +2,7 @@
 #include <stdio.h>
 
 // Write array to a file
-bool_t write_array(char* output_file_name, void* array, size_t size)
-{
+bool_t write_array(char *output_file_name, void *array, size_t size) {
   FILE *file = fopen(output_file_name, "wb");
   if (file == NULL) {
     fprintf(stderr, "Error opening file %s\n", output_file_name);
diff --git a/lib/lib.c b/lib/lib.c
index c1f26fc..1376a77 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -1,4 +1,4 @@
 #include <lib/lib.h>
 
-const uint32_t SCREEN_WIDTH  = 640;
+const uint32_t SCREEN_WIDTH = 640;
 const uint32_t SCREEN_HEIGHT = 480;
diff --git a/lib/png.c b/lib/png.c
index 5da0b95..1264a5a 100644
--- a/lib/png.c
+++ b/lib/png.c
@@ -1,17 +1,16 @@
 #include <lib/png.h>
 
-#include <stdio.h>
 #include <png.h>
+#include <stdio.h>
 
 // Save bitmap to file
-void save_png(Bitmap* bitmap, char* fname)
-{
+void save_png(Bitmap *bitmap, char *fname) {
   FILE *fp;
   png_structp png_ptr = NULL;
   png_infop info_ptr = NULL;
   PixelSize_t pixel_size = 3;
   PixelDepth_t pixel_depth = 8;
-  png_byte ** row_pointers = NULL;
+  png_byte **row_pointers = NULL;
   size_t x, y;
   fp = fopen(fname, "wb");
   if (fp == NULL) {
@@ -35,20 +34,19 @@ void save_png(Bitmap* bitmap, char* fname)
     fclose(fp);
     return;
   }
-  png_set_IHDR(png_ptr, info_ptr,
-	       bitmap->width, bitmap->height,
-	       pixel_depth,
-	       PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
-	       PNG_FILTER_TYPE_DEFAULT);
+  png_set_IHDR(png_ptr, info_ptr, bitmap->width, bitmap->height, pixel_depth,
+               PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
+               PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
 
-  row_pointers = png_malloc(png_ptr, bitmap->height*sizeof(png_byte*));
+  row_pointers = png_malloc(png_ptr, bitmap->height * sizeof(png_byte *));
   for (y = 0; y < bitmap->height; y++) {
-    png_byte *row = png_malloc(png_ptr, sizeof(PixelChannel_t)*bitmap->width*pixel_size);
+    png_byte *row = png_malloc(png_ptr, sizeof(PixelChannel_t) * bitmap->width *
+                                            pixel_size);
     row_pointers[y] = row;
     for (x = 0; x < bitmap->width; x++) {
-      *row++ = bitmap->image_buffer[x + y*bitmap->width].red;
-      *row++ = bitmap->image_buffer[x + y*bitmap->width].green;
-      *row++ = bitmap->image_buffer[x + y*bitmap->width].blue;
+      *row++ = bitmap->image_buffer[x + y * bitmap->width].red;
+      *row++ = bitmap->image_buffer[x + y * bitmap->width].green;
+      *row++ = bitmap->image_buffer[x + y * bitmap->width].blue;
     }
   }
   png_init_io(png_ptr, fp);
@@ -61,4 +59,3 @@ void save_png(Bitmap* bitmap, char* fname)
   png_destroy_write_struct(&png_ptr, &info_ptr);
   fclose(fp);
 }
-
diff --git a/lib/seg/mask_data.c b/lib/seg/mask_data.c
index d1713e2..144c7ca 100644
--- a/lib/seg/mask_data.c
+++ b/lib/seg/mask_data.c
@@ -4,9 +4,8 @@
 #include <stdio.h>
 
 // Allocate Mask Data for Label
-MaskData* create_mask_data(MaskData_t label)
-{
-  MaskData *data = (MaskData*)malloc(sizeof(MaskData));
+MaskData *create_mask_data(MaskData_t label) {
+  MaskData *data = (MaskData *)malloc(sizeof(MaskData));
   data->label = label;
   data->area = 0;
   data->perimeter = 0;
@@ -14,79 +13,68 @@ MaskData* create_mask_data(MaskData_t label)
 }
 
 // Compare mask data labels
-bool_t compare_labels(MaskData* left, MaskData* right)
-{
+bool_t compare_labels(MaskData *left, MaskData *right) {
   return left->label < right->label;
 }
 
 // Create AVL Mask node
-AVLNode* create_avl_mask_node(MaskData* data)
-{
-  return create_avl_node((void*)data, (AvlComparator)compare_labels);
+AVLNode *create_avl_mask_node(MaskData *data) {
+  return create_avl_node((void *)data, (AvlComparator)compare_labels);
 }
 
 // Insert MaskData into the AVL Tree
-Result insert_mask(AVLNode* node, MaskData* data)
-{
-  return avl_insert(node, (void*)data, (AvlComparator)compare_labels);
+Result insert_mask(AVLNode *node, MaskData *data) {
+  return avl_insert(node, (void *)data, (AvlComparator)compare_labels);
 }
 
 // Allocate a label's Mask data in a tree
 //  If it already exists, skip the allocation
-AVLNode* insert_mask_alloc(AVLNode* node, MaskData_t label)
-{
-  MaskData* data = create_mask_data(label);
+AVLNode *insert_mask_alloc(AVLNode *node, MaskData_t label) {
+  MaskData *data = create_mask_data(label);
   Result result = insert_mask(node, data);
   if (!result.success) {
     free(data);
   }
-  return (AVLNode*)result.data;
+  return (AVLNode *)result.data;
 }
 
 // Print AVL Node Mask Data Label
-void print_label(AVLNode* root)
-{
+void print_label(AVLNode *root) {
   if (root != NULL) {
     print_label(root->left);
-    MaskData* data = root->data;
+    MaskData *data = root->data;
     printf("%d: (%zu, %zu) ", data->label, data->area, data->perimeter);
     print_label(root->right);
   }
 }
 
 // Increase the label's area
-bool_t increase_label_area(AVLNode* root, MaskData_t label)
-{
+bool_t increase_label_area(AVLNode *root, MaskData_t label) {
   if (root == NULL) {
     return FALSE;
   }
-  MaskData* data = (MaskData*)root->data;
+  MaskData *data = (MaskData *)root->data;
   if (data->label == label) {
     data->area++;
-  }
-  else if (data->label > label) {
+  } else if (data->label > label) {
     return increase_label_area(root->left, label);
-  }
-  else if (data->label < label) {
+  } else if (data->label < label) {
     return increase_label_area(root->right, label);
   }
   return TRUE;
 }
 
 // Increase the label's perimeter
-bool_t increase_label_perimeter(AVLNode* root, MaskData_t label)
-{
+bool_t increase_label_perimeter(AVLNode *root, MaskData_t label) {
   if (root == NULL) {
     return FALSE;
   }
-  MaskData* data = (MaskData*)root->data;
+  MaskData *data = (MaskData *)root->data;
   if (data->label == label) {
     data->perimeter++;
-  }
-  else if (data->label > label) {
+  } else if (data->label > label) {
     return increase_label_perimeter(root->left, label);
-  }
-  else if (data->label < label) {
+  } else if (data->label < label) {
     return increase_label_perimeter(root->right, label);
   }
   return TRUE;
@@ -94,9 +82,8 @@ bool_t increase_label_perimeter(AVLNode* root, MaskData_t label)
 
 // Increase the label's area
 //  Create an AVL node if it doesn't exist
-AVLNode* increase_label_area_alloc(AVLNode* root, MaskData_t label)
-{
-  AVLNode* new_root = root;
+AVLNode *increase_label_area_alloc(AVLNode *root, MaskData_t label) {
+  AVLNode *new_root = root;
   bool_t success = increase_label_area(new_root, label);
   if (success == FALSE) {
     new_root = insert_mask_alloc(new_root, label);
@@ -107,9 +94,8 @@ AVLNode* increase_label_area_alloc(AVLNode* root, MaskData_t label)
 
 // Increase the label's perimeter
 //  Create an AVL node if it doesn't exist
-AVLNode* increase_label_perimeter_alloc(AVLNode* root, MaskData_t label)
-{
-  AVLNode* new_root = root;
+AVLNode *increase_label_perimeter_alloc(AVLNode *root, MaskData_t label) {
+  AVLNode *new_root = root;
   bool_t success = increase_label_perimeter(new_root, label);
   if (success == FALSE) {
     new_root = insert_mask_alloc(new_root, label);
@@ -119,30 +105,27 @@ AVLNode* increase_label_perimeter_alloc(AVLNode* root, MaskData_t label)
 }
 
 // Comparison of MaskData_ts
-bool_t compare_image_mask_data_t(MaskData_t* s1, MaskData_t* s2)
-{
+bool_t compare_image_mask_data_t(MaskData_t *s1, MaskData_t *s2) {
   return *s1 < *s2;
 }
 
 // In-order traversal print pointer
-void print_in_order_image_mask_data_t(AVLNode* root)
-{
+void print_in_order_image_mask_data_t(AVLNode *root) {
   if (root != NULL) {
     print_in_order_image_mask_data_t(root->left);
-    printf("%d ", *((MaskData_t*)root->data));
+    printf("%d ", *((MaskData_t *)root->data));
     print_in_order_image_mask_data_t(root->right);
   }
 }
 
 // Check if MaskData_t in AVLTree with MaskData_t* data
-bool_t in_image_mask_data_t_tree(AVLNode* root, MaskData_t value)
-{
+bool_t in_image_mask_data_t_tree(AVLNode *root, MaskData_t value) {
   if (root == NULL) {
     return FALSE;
   }
-  if (*((MaskData_t*)root->data) == value) {
+  if (*((MaskData_t *)root->data) == value) {
     return TRUE;
-  } else if (value < *((MaskData_t*)root->data)) {
+  } else if (value < *((MaskData_t *)root->data)) {
     return in_image_mask_data_t_tree(root->left, value);
   } else {
     return in_image_mask_data_t_tree(root->right, value);
@@ -151,37 +134,41 @@ bool_t in_image_mask_data_t_tree(AVLNode* root, MaskData_t value)
 
 // Filter out small masks
 //  Assumption: Contiguous labeling
-AVLNode* get_small_labels(AVLNode* removal_tree, AVLNode* label_tree, size_t min_area, size_t min_perimeter)
-{
-  AVLNode* return_tree = removal_tree;
+AVLNode *get_small_labels(AVLNode *removal_tree, AVLNode *label_tree,
+                          size_t min_area, size_t min_perimeter) {
+  AVLNode *return_tree = removal_tree;
   if (label_tree != NULL) {
-    return_tree = get_small_labels(return_tree, label_tree->left, min_area, min_perimeter);
-    MaskData* node_data = (MaskData*)label_tree->data;
-    if ((node_data->area < min_area) || (node_data->perimeter < min_perimeter)) {
+    return_tree = get_small_labels(return_tree, label_tree->left, min_area,
+                                   min_perimeter);
+    MaskData *node_data = (MaskData *)label_tree->data;
+    if ((node_data->area < min_area) ||
+        (node_data->perimeter < min_perimeter)) {
       // Insert
-      Result result = avl_insert(return_tree, &node_data->label, (bool_t (*)(void*,void*))compare_image_mask_data_t);
+      Result result =
+          avl_insert(return_tree, &node_data->label,
+                     (bool_t(*)(void *, void *))compare_image_mask_data_t);
       if (result.success) {
-	return_tree = result.data;
+        return_tree = result.data;
       }
     }
-    return_tree = get_small_labels(return_tree, label_tree->right, min_area, min_perimeter);
+    return_tree = get_small_labels(return_tree, label_tree->right, min_area,
+                                   min_perimeter);
   }
   return return_tree;
 }
 
 // Get mask label data
-AVLNode *get_mask_data(Mask *mask)
-{
+AVLNode *get_mask_data(Mask *mask) {
   uint32_t width = mask->width;
   uint32_t height = mask->height;
-  AVLNode* root = NULL;
+  AVLNode *root = NULL;
   for (size_t y = 0; y < height; y++) {
     for (size_t x = 0; x < width; x++) {
       if (mask->image[y][x] != 0) {
-	root = increase_label_area_alloc(root, mask->image[y][x]);
-	if (is_on_mask_boundary(mask, x, y)) {
-	  increase_label_perimeter(root, mask->image[y][x]);
-	}
+        root = increase_label_area_alloc(root, mask->image[y][x]);
+        if (is_on_mask_boundary(mask, x, y)) {
+          increase_label_perimeter(root, mask->image[y][x]);
+        }
       }
     }
   }
@@ -189,17 +176,16 @@ AVLNode *get_mask_data(Mask *mask)
 }
 
 // Filter out small masks in mask
-void filter_small_masks(Mask *mask, size_t min_area, size_t min_perimeter)
-{
+void filter_small_masks(Mask *mask, size_t min_area, size_t min_perimeter) {
   uint32_t width = mask->width;
   uint32_t height = mask->height;
-  AVLNode* root = get_mask_data(mask);
-  AVLNode* small_label_tree = NULL;
+  AVLNode *root = get_mask_data(mask);
+  AVLNode *small_label_tree = NULL;
   small_label_tree = get_small_labels(NULL, root, min_area, min_perimeter);
   for (size_t y = 0; y < height; y++) {
     for (size_t x = 0; x < width; x++) {
       if (in_image_mask_data_t_tree(small_label_tree, mask->image[y][x])) {
-	mask->image[y][x] = 0; 
+        mask->image[y][x] = 0;
       }
     }
   }
diff --git a/lib/seg/util.c b/lib/seg/util.c
index 7b8d317..8af8199 100644
--- a/lib/seg/util.c
+++ b/lib/seg/util.c
@@ -1,43 +1,41 @@
-#include <lib/seg/util.h>
+#include <assert.h>
 #include <lib/algo/flood_fill.h>
 #include <lib/png.h>
-#include <tiffio.h>
-#include <assert.h>
+#include <lib/seg/util.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <tiffio.h>
 
 // Suppress Tiff Warnings
-void TiffDummyHandler(const char* module, const char* fmt, va_list ap)
-{
-    // ignore errors and warnings (or handle them your own way)
+void TiffDummyHandler(const char *module, const char *fmt, va_list ap) {
+  // ignore errors and warnings (or handle them your own way)
 }
 
 // Determine if coordinate is on a mask boundary
 //  Assumes mask is (WxH)
-bool_t is_on_mask_boundary(Mask* mask, size_t x, size_t y)
-{
+bool_t is_on_mask_boundary(Mask *mask, size_t x, size_t y) {
   MaskData_t current_value = mask->image[y][x];
 
   // Left neighbor
   if (x != 0) {
-    if (mask->image[y][x-1] != current_value) {
+    if (mask->image[y][x - 1] != current_value) {
       return TRUE;
     }
   }
   // Right neighbor
-  if ((x+1) != mask->width) {
-    if (mask->image[y][x+1] != current_value) {
+  if ((x + 1) != mask->width) {
+    if (mask->image[y][x + 1] != current_value) {
       return TRUE;
     }
   }
   if (y != 0) {
-    if (mask->image[y-1][x] != current_value) {
+    if (mask->image[y - 1][x] != current_value) {
       return TRUE;
     }
   }
-  if ((y+1) != mask->height) {
-    if (mask->image[y+1][x] != current_value) {
+  if ((y + 1) != mask->height) {
+    if (mask->image[y + 1][x] != current_value) {
       return TRUE;
     }
   }
@@ -45,40 +43,39 @@ bool_t is_on_mask_boundary(Mask* mask, size_t x, size_t y)
 }
 
 // Dilate masks by one 4-connected pixel
-MaskData_t* _dilate(const Mask* mask)
-{
+MaskData_t *_dilate(const Mask *mask) {
   uint32_t width = mask->width;
   uint32_t height = mask->height;
   Mask *new_mask = create_image_mask(width, height);
   for (size_t y = 0; y < height; y++) {
     for (size_t x = 0; x < width; x++) {
       if (mask->image[y][x] != 0) {
-	new_mask->image[y][x] = mask->image[y][x];
-	continue;
+        new_mask->image[y][x] = mask->image[y][x];
+        continue;
       }
       if (x != 0) {
-	if (mask->image[y][x-1] != 0) {
-	  new_mask->image[y][x] = mask->image[y][x-1];
-	  continue;
-	}
+        if (mask->image[y][x - 1] != 0) {
+          new_mask->image[y][x] = mask->image[y][x - 1];
+          continue;
+        }
       }
-      if ((x+1) != width) {
-	if (mask->image[y][x+1] != 0) {
-	  new_mask->image[y][x] = mask->image[y][x+1];
-	  continue;
-	}
+      if ((x + 1) != width) {
+        if (mask->image[y][x + 1] != 0) {
+          new_mask->image[y][x] = mask->image[y][x + 1];
+          continue;
+        }
       }
       if (y != 0) {
-	if (mask->image[y-1][x] != 0) {
-	  new_mask->image[y][x] = mask->image[y-1][x];
-	  continue;
-	}
+        if (mask->image[y - 1][x] != 0) {
+          new_mask->image[y][x] = mask->image[y - 1][x];
+          continue;
+        }
       }
-      if ((y+1) != height) {
-	if (mask->image[y+1][x] != 0) {
-	  new_mask->image[y][x] = mask->image[y+1][x];
-	  continue;
-	}
+      if ((y + 1) != height) {
+        if (mask->image[y + 1][x] != 0) {
+          new_mask->image[y][x] = mask->image[y + 1][x];
+          continue;
+        }
       }
     }
   }
@@ -89,49 +86,48 @@ MaskData_t* _dilate(const Mask* mask)
 }
 
 // Dilate masks by one 4-connected pixel
-void dilate(Mask* mask)
-{
+void dilate(Mask *mask) {
   MaskData_t *new_mask = _dilate(mask);
   if (new_mask != NULL) {
     free(mask->image[0]);
     for (size_t y = 0; y < mask->height; y++) {
-      mask->image[y] = &(new_mask[y*mask->width]);
+      mask->image[y] = &(new_mask[y * mask->width]);
     }
   }
 }
 
 // Erode masks by one 4-connected pixel
-MaskData_t* _erode(const Mask* mask)
-{
+MaskData_t *_erode(const Mask *mask) {
   uint32_t width = mask->width;
   uint32_t height = mask->height;
   Mask *new_mask = create_image_mask(width, height);
-  memcpy(new_mask->image[0], mask->image[0], width*height*sizeof(MaskData_t));
+  memcpy(new_mask->image[0], mask->image[0],
+         width * height * sizeof(MaskData_t));
   for (size_t y = 0; y < height; y++) {
     for (size_t x = 0; x < width; x++) {
       if (x != 0) {
-	if (mask->image[y][x-1] == 0) {
-	  new_mask->image[y][x] = 0;
-	  continue;
-	}
+        if (mask->image[y][x - 1] == 0) {
+          new_mask->image[y][x] = 0;
+          continue;
+        }
       }
-      if ((x+1) != width) {
-	if (mask->image[y][x+1] == 0) {
-	  new_mask->image[y][x] = 0;
-	  continue;
-	}
+      if ((x + 1) != width) {
+        if (mask->image[y][x + 1] == 0) {
+          new_mask->image[y][x] = 0;
+          continue;
+        }
       }
       if (y != 0) {
-	if (mask->image[y-1][x] == 0) {
-	  new_mask->image[y][x] = 0;
-	  continue;
-	}
+        if (mask->image[y - 1][x] == 0) {
+          new_mask->image[y][x] = 0;
+          continue;
+        }
       }
-      if ((y+1) != height) {
-	if (mask->image[y+1][x] == 0) {
-	  new_mask->image[y][x] = 0;
-	  continue;
-	}
+      if ((y + 1) != height) {
+        if (mask->image[y + 1][x] == 0) {
+          new_mask->image[y][x] = 0;
+          continue;
+        }
       }
     }
   }
@@ -142,20 +138,18 @@ MaskData_t* _erode(const Mask* mask)
 }
 
 // Erode masks by one 4-connected pixel
-void erode(Mask* mask)
-{
+void erode(Mask *mask) {
   MaskData_t *new_mask = _erode(mask);
   if (new_mask != NULL) {
     free(mask->image[0]);
     for (size_t y = 0; y < mask->height; y++) {
-      mask->image[y] = &(new_mask[y*mask->width]);
+      mask->image[y] = &(new_mask[y * mask->width]);
     }
   }
 }
 
 // Close up masks by N-pixels
-MaskData_t* _closeup(Mask* mask, size_t num_pixels)
-{
+MaskData_t *_closeup(Mask *mask, size_t num_pixels) {
   Mask *new_mask = create_image_mask(mask->width, mask->height);
   memcpy(new_mask->image[0], mask->image[0],
          mask->width * mask->height * sizeof(MaskData_t));
@@ -169,7 +163,7 @@ MaskData_t* _closeup(Mask* mask, size_t num_pixels)
     for (size_t x = 0; x < mask->width; x++) {
       if (mask->image[y][x] != 0) {
         if (new_mask->image[y][x] != mask->image[y][x]) {
-	  new_mask->image[y][x] = mask->image[y][x];
+          new_mask->image[y][x] = mask->image[y][x];
         }
       }
     }
@@ -182,13 +176,12 @@ MaskData_t* _closeup(Mask* mask, size_t num_pixels)
 
 // Close up masks by N-pixels
 //  Update pointer
-void closeup(Mask* mask, size_t num_pixels)
-{
+void closeup(Mask *mask, size_t num_pixels) {
   MaskData_t *new_mask = _closeup(mask, num_pixels);
   if (new_mask != NULL) {
     free(mask->image[0]);
     for (size_t y = 0; y < mask->height; y++) {
-      mask->image[y] = &(new_mask[y*mask->width]);
+      mask->image[y] = &(new_mask[y * mask->width]);
     }
   }
 }
@@ -196,15 +189,14 @@ void closeup(Mask* mask, size_t num_pixels)
 // Combine Label Masks
 // For all empty spaces in the destination, put the extra label if it exists
 // Allocates an array if destination is unallocated
-Mask* combine_masks(Mask *destination, Mask *extra_labels)
-{
+Mask *combine_masks(Mask *destination, Mask *extra_labels) {
   if (destination == NULL) {
     destination = create_image_mask(extra_labels->width, extra_labels->height);
   }
   for (size_t y = 0; y < destination->height; y++) {
     for (size_t x = 0; x < destination->width; x++) {
       if (destination->image[y][x] == 0) {
-	destination->image[y][x] = extra_labels->image[y][x];
+        destination->image[y][x] = extra_labels->image[y][x];
       }
     }
   }
@@ -214,8 +206,7 @@ Mask* combine_masks(Mask *destination, Mask *extra_labels)
 // Process Tif File to Labels
 //  width, height will be overwritten with image dimensions
 //  starting_label_p will be incremented for each label found in the image
-Mask *tif_to_labels(char *tif_file_name, MaskData_t *starting_label_p)
-{
+Mask *tif_to_labels(char *tif_file_name, MaskData_t *starting_label_p) {
   uint32_t width = 0;
   uint32_t height = 0;
   TIFFSetWarningHandler(TiffDummyHandler);
@@ -233,10 +224,10 @@ Mask *tif_to_labels(char *tif_file_name, MaskData_t *starting_label_p)
 
   tmsize_t STRIP_LENGTH = TIFFStripSize(tif);
   tmsize_t STRIP_COUNT = TIFFNumberOfStrips(tif);
-  channels = (STRIP_LENGTH*STRIP_COUNT)/(width*height);
+  channels = (STRIP_LENGTH * STRIP_COUNT) / (width * height);
 
   //-TIFF-LOAD-DATA--------------------------------
-  void* buffer = malloc(STRIP_LENGTH*sizeof(ImageData_t));
+  void *buffer = malloc(STRIP_LENGTH * sizeof(ImageData_t));
   if (buffer == NULL) {
     fprintf(stderr, "Memory allocation error\n");
     TIFFClose(tif);
@@ -253,7 +244,7 @@ Mask *tif_to_labels(char *tif_file_name, MaskData_t *starting_label_p)
     tmsize_t strip_size = TIFFReadRawStrip(tif, y, buffer, STRIP_LENGTH);
     assert(strip_size == STRIP_LENGTH);
     for (size_t x = 0; x < STRIP_LENGTH; x++) {
-      image->image[0][0][x + y*STRIP_LENGTH] = ((ImageData_t*)buffer)[x];
+      image->image[0][0][x + y * STRIP_LENGTH] = ((ImageData_t *)buffer)[x];
     }
   }
   free(buffer);
@@ -271,7 +262,8 @@ Mask *tif_to_labels(char *tif_file_name, MaskData_t *starting_label_p)
   //  Increase label for each success
   for (size_t y = 0; y < height; y++) {
     for (size_t x = 0; x < width; x++) {
-      if(flood(image->image[0][0], im_data->image[0], width, height, channels, x, y, image->image[y][x], *starting_label_p)) {
+      if (flood(image->image[0][0], im_data->image[0], width, height, channels,
+                x, y, image->image[y][x], *starting_label_p)) {
         *starting_label_p += 1;
       }
     }
@@ -279,30 +271,29 @@ Mask *tif_to_labels(char *tif_file_name, MaskData_t *starting_label_p)
   free_image(image);
   TIFFClose(tif);
   return im_data;
-  //return labels;
+  // return labels;
 }
 
 // Convert mask to bitmap
-Bitmap* image_mask_data_to_bitmap(const Mask* mask)
-{
+Bitmap *image_mask_data_to_bitmap(const Mask *mask) {
   MaskData_t *buffer = mask->image[0];
   uint32_t width = mask->width;
   uint32_t height = mask->height;
-  Pixel* out_buffer = (Pixel*)calloc(width*height, sizeof(Pixel));
+  Pixel *out_buffer = (Pixel *)calloc(width * height, sizeof(Pixel));
   if (out_buffer == NULL) {
     return NULL;
   }
-  Bitmap* bitmap = (Bitmap*)malloc(sizeof(Bitmap));
+  Bitmap *bitmap = (Bitmap *)malloc(sizeof(Bitmap));
   if (bitmap == NULL) {
     free(out_buffer);
     return NULL;
   }
   for (size_t y = 0; y < height; y++) {
     for (size_t x = 0; x < width; x++) {
-      size_t coord = x + y*width;
-      ImageData_t red   = (buffer[coord] & 0xF00) >> 4*2;
-      ImageData_t green = (buffer[coord] & 0x0F0) >> 4*1;
-      ImageData_t blue  = (buffer[coord] & 0x00F) >> 4*0;
+      size_t coord = x + y * width;
+      ImageData_t red = (buffer[coord] & 0xF00) >> 4 * 2;
+      ImageData_t green = (buffer[coord] & 0x0F0) >> 4 * 1;
+      ImageData_t blue = (buffer[coord] & 0x00F) >> 4 * 0;
       out_buffer[coord].red = red | (red << 4);
       out_buffer[coord].green = green | (green << 4);
       out_buffer[coord].blue = blue | (blue << 4);
@@ -315,19 +306,22 @@ Bitmap* image_mask_data_to_bitmap(const Mask* mask)
 }
 
 // Reduce a mask to the contiguous regions
-MaskData_t* _reduce_contiguous_regions(MaskData_t* masks, uint32_t width, uint32_t height, MaskData_t* total_labels)
-{
+MaskData_t *_reduce_contiguous_regions(MaskData_t *masks, uint32_t width,
+                                       uint32_t height,
+                                       MaskData_t *total_labels) {
   MaskData_t starting_label = 1;
-  MaskData_t* new_masks = (MaskData_t*)calloc(width*height, sizeof(MaskData_t));
+  MaskData_t *new_masks =
+      (MaskData_t *)calloc(width * height, sizeof(MaskData_t));
   if (new_masks == NULL) {
     return NULL;
   }
   for (size_t y = 0; y < height; y++) {
     for (size_t x = 0; x < width; x++) {
-      size_t coord = x + y*width;
+      size_t coord = x + y * width;
       ImageData_t channels = 2; // MaskData_t = 2*uint8_t
-      if (flood((ImageData_t*)masks, new_masks, width, height, channels, x, y, &(((ImageData_t*)masks)[coord*channels]), starting_label)) {
-	starting_label++;
+      if (flood((ImageData_t *)masks, new_masks, width, height, channels, x, y,
+                &(((ImageData_t *)masks)[coord * channels]), starting_label)) {
+        starting_label++;
       }
     }
   }
@@ -340,16 +334,16 @@ MaskData_t* _reduce_contiguous_regions(MaskData_t* masks, uint32_t width, uint32
 // Reduce a mask to the contiguous regions
 //  Automatically update pointer to contiguous mask
 //  Freeing previous mask
-void reduce_contiguous_regions(Mask* mask, MaskData_t* total_labels)
-{
+void reduce_contiguous_regions(Mask *mask, MaskData_t *total_labels) {
   if (mask == NULL) {
     return;
   }
-  MaskData_t* new_masks = _reduce_contiguous_regions(mask->image[0], mask->width, mask->height, total_labels);
+  MaskData_t *new_masks = _reduce_contiguous_regions(
+      mask->image[0], mask->width, mask->height, total_labels);
   if (new_masks != NULL) {
     free(mask->image[0]);
     for (size_t y = 0; y < mask->height; y++) {
-      mask->image[y] = &new_masks[y*mask->width];
+      mask->image[y] = &new_masks[y * mask->width];
     }
   }
 }
diff --git a/lib/time.c b/lib/time.c
index 623a696..e31cbb7 100644
--- a/lib/time.c
+++ b/lib/time.c
@@ -3,11 +3,9 @@
 // Difference in Time
 // Compute the difference between timespec structs
 double diff_time(const struct timespec *time1, const struct timespec *time0) {
-  return (time1->tv_sec - time0->tv_sec)
-      + (time1->tv_nsec - time0->tv_nsec) / 1000000000.0;
+  return (time1->tv_sec - time0->tv_sec) +
+         (time1->tv_nsec - time0->tv_nsec) / 1000000000.0;
 }
 
 // Get Current Time
-void get_time(struct timespec *ts) {
-  timespec_get(ts, TIME_UTC);
-}
+void get_time(struct timespec *ts) { timespec_get(ts, TIME_UTC); }
-- 
cgit v1.2.1