diff options
author | Christian C <cc@localhost> | 2025-04-01 17:38:32 -0700 |
---|---|---|
committer | Christian C <cc@localhost> | 2025-04-01 17:38:32 -0700 |
commit | a18cea2fef7aa1545c9a984b60919541b26a6f84 (patch) | |
tree | d1ab991e28b701bdfdc9035704389ad6c57391c9 /lib/seg | |
parent | 2bc54ac42b831a7dfcba26c2d12ba002f80a5e40 (diff) |
Clang Format
Diffstat (limited to 'lib/seg')
-rw-r--r-- | lib/seg/mask_data.c | 122 | ||||
-rw-r--r-- | lib/seg/util.c | 188 |
2 files changed, 145 insertions, 165 deletions
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]; } } } |