aboutsummaryrefslogtreecommitdiff
path: root/lib/seg
diff options
context:
space:
mode:
authorChristian C <cc@localhost>2025-03-22 22:27:40 -0700
committerChristian C <cc@localhost>2025-03-22 22:27:40 -0700
commit16422831ed49c573c1b8a43ba907bceb00fd5eea (patch)
tree1c2d86d5eecf6d1b7b4951f0b20f8c86e6939ac2 /lib/seg
parent123464e36f2a151d820e08db7b4b426ca2b3657a (diff)
Named Types
Diffstat (limited to 'lib/seg')
-rw-r--r--lib/seg/mask_data.c44
-rw-r--r--lib/seg/util.c72
2 files changed, 58 insertions, 58 deletions
diff --git a/lib/seg/mask_data.c b/lib/seg/mask_data.c
index 8c4b037..dc3a811 100644
--- a/lib/seg/mask_data.c
+++ b/lib/seg/mask_data.c
@@ -4,7 +4,7 @@
#include <stdio.h>
// Allocate Mask Data for Label
-struct MaskData* create_mask_data(uint16_t label)
+struct MaskData* create_mask_data(ImageMaskData_t label)
{
struct MaskData *data = (struct MaskData*)malloc(sizeof(struct MaskData));
data->label = label;
@@ -90,7 +90,7 @@ struct Result insert_mask(struct AVLNode* node, struct MaskData* data)
// Allocate a label's Mask data in a tree
// If it already exists, skip the allocation
-struct AVLNode* insert_mask_alloc(struct AVLNode* node, uint16_t label)
+struct AVLNode* insert_mask_alloc(struct AVLNode* node, ImageMaskData_t label)
{
struct MaskData* data = create_mask_data(label);
struct Result result = insert_mask(node, data);
@@ -112,7 +112,7 @@ void print_label(struct AVLNode* root)
}
// Increase the label's area
-bool_t increase_label_area(struct AVLNode* root, uint16_t label)
+bool_t increase_label_area(struct AVLNode* root, ImageMaskData_t label)
{
if (root == NULL) {
return FALSE;
@@ -131,7 +131,7 @@ bool_t increase_label_area(struct AVLNode* root, uint16_t label)
}
// Increase the label's perimeter
-bool_t increase_label_perimeter(struct AVLNode* root, uint16_t label)
+bool_t increase_label_perimeter(struct AVLNode* root, ImageMaskData_t label)
{
if (root == NULL) {
return FALSE;
@@ -151,7 +151,7 @@ bool_t increase_label_perimeter(struct AVLNode* root, uint16_t label)
// Increase the label's area
// Create an AVL node if it doesn't exist
-struct AVLNode* increase_label_area_alloc(struct AVLNode* root, uint16_t label)
+struct AVLNode* increase_label_area_alloc(struct AVLNode* root, ImageMaskData_t label)
{
struct AVLNode* new_root = root;
bool_t success = increase_label_area(new_root, label);
@@ -164,7 +164,7 @@ struct AVLNode* increase_label_area_alloc(struct AVLNode* root, uint16_t label)
// Increase the label's perimeter
// Create an AVL node if it doesn't exist
-struct AVLNode* increase_label_perimeter_alloc(struct AVLNode* root, uint16_t label)
+struct AVLNode* increase_label_perimeter_alloc(struct AVLNode* root, ImageMaskData_t label)
{
struct AVLNode* new_root = root;
bool_t success = increase_label_perimeter(new_root, label);
@@ -175,34 +175,34 @@ struct AVLNode* increase_label_perimeter_alloc(struct AVLNode* root, uint16_t la
return new_root;
}
-// Comparison of uint16_ts
-bool_t compare_uint16_t(uint16_t* s1, uint16_t* s2)
+// Comparison of ImageMaskData_ts
+bool_t compare_image_mask_data_t(ImageMaskData_t* s1, ImageMaskData_t* s2)
{
return *s1 < *s2;
}
// In-order traversal print pointer
-void print_in_order_uint16_t(struct AVLNode* root)
+void print_in_order_image_mask_data_t(struct AVLNode* root)
{
if (root != NULL) {
- print_in_order_uint16_t(root->left);
- printf("%d ", *((uint16_t*)root->data));
- print_in_order_uint16_t(root->right);
+ print_in_order_image_mask_data_t(root->left);
+ printf("%d ", *((ImageMaskData_t*)root->data));
+ print_in_order_image_mask_data_t(root->right);
}
}
-// Check if uint16_t in AVLTree with uint16_t* data
-bool_t in_uint16_t_tree(struct AVLNode* root, uint16_t value)
+// Check if ImageMaskData_t in AVLTree with ImageMaskData_t* data
+bool_t in_image_mask_data_t_tree(struct AVLNode* root, ImageMaskData_t value)
{
if (root == NULL) {
return FALSE;
}
- if (*((uint16_t*)root->data) == value) {
+ if (*((ImageMaskData_t*)root->data) == value) {
return TRUE;
- } else if (value < *((uint16_t*)root->data)) {
- return in_uint16_t_tree(root->left, value);
+ } else if (value < *((ImageMaskData_t*)root->data)) {
+ return in_image_mask_data_t_tree(root->left, value);
} else {
- return in_uint16_t_tree(root->right, value);
+ return in_image_mask_data_t_tree(root->right, value);
}
}
@@ -216,7 +216,7 @@ struct AVLNode* get_small_labels(struct AVLNode* removal_tree, struct AVLNode* l
struct MaskData* node_data = (struct MaskData*)label_tree->data;
if ((node_data->area < min_area) || (node_data->perimeter < min_perimeter)) {
// Insert
- struct Result result = avl_insert(return_tree, &node_data->label, (bool_t (*)(void*,void*))compare_uint16_t);
+ struct 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;
}
@@ -227,7 +227,7 @@ struct AVLNode* get_small_labels(struct AVLNode* removal_tree, struct AVLNode* l
}
// Get mask label data
-struct AVLNode* get_mask_data(uint16_t* masks, uint32_t width, uint32_t height)
+struct AVLNode* get_mask_data(ImageMaskData_t* masks, uint32_t width, uint32_t height)
{
struct AVLNode* root = NULL;
for (size_t y = 0; y < height; y++) {
@@ -245,7 +245,7 @@ struct AVLNode* get_mask_data(uint16_t* masks, uint32_t width, uint32_t height)
}
// Filter out small masks in mask
-void filter_small_masks(uint16_t* masks, uint32_t width, uint32_t height, size_t min_area, size_t min_perimeter)
+void filter_small_masks(ImageMaskData_t* masks, uint32_t width, uint32_t height, size_t min_area, size_t min_perimeter)
{
struct AVLNode* root = get_mask_data(masks, width, height);
struct AVLNode* small_label_tree = NULL;
@@ -253,7 +253,7 @@ void filter_small_masks(uint16_t* masks, uint32_t width, uint32_t height, size_t
for (size_t y = 0; y < height; y++) {
for (size_t x = 0; x < width; x++) {
size_t coord = x + y*width;
- if (in_uint16_t_tree(small_label_tree, masks[coord])) {
+ if (in_image_mask_data_t_tree(small_label_tree, masks[coord])) {
masks[coord] = 0;
}
}
diff --git a/lib/seg/util.c b/lib/seg/util.c
index 677e8f5..4290d46 100644
--- a/lib/seg/util.c
+++ b/lib/seg/util.c
@@ -1,4 +1,4 @@
- #include <lib/seg/util.h>
+#include <lib/seg/util.h>
#include <lib/algo/flood_fill.h>
#include <lib/png.h>
#include <tiffio.h>
@@ -21,11 +21,11 @@ size_t xy_to_coord(size_t x, size_t y, uint32_t width, uint32_t height)
// Determine if coordinate is on a mask boundary
// Assumes mask is (WxH)
-bool_t is_on_mask_boundary(uint16_t* mask, uint32_t width, uint32_t height, size_t x, size_t y)
+bool_t is_on_mask_boundary(ImageMaskData_t* mask, uint32_t width, uint32_t height, size_t x, size_t y)
{
size_t starting_coord = xy_to_coord(x, y, width, height);
size_t proposed_position;
- uint16_t current_value = mask[starting_coord];
+ ImageMaskData_t current_value = mask[starting_coord];
// Left neighbor
if (x != 0) {
@@ -57,9 +57,9 @@ bool_t is_on_mask_boundary(uint16_t* mask, uint32_t width, uint32_t height, size
}
// Dilate masks by one 4-connected pixel
-uint16_t* _dilate(uint16_t* mask, uint32_t width, uint32_t height)
+ImageMaskData_t* _dilate(ImageMaskData_t* mask, uint32_t width, uint32_t height)
{
- uint16_t *new_mask = (uint16_t*)calloc(width*height,sizeof(uint16_t));
+ ImageMaskData_t *new_mask = (ImageMaskData_t*)calloc(width*height,sizeof(ImageMaskData_t));
for (size_t y = 0; y < height; y++) {
for (size_t x = 0; x < width; x++) {
size_t current_position = xy_to_coord(x, y, width, height);
@@ -102,9 +102,9 @@ uint16_t* _dilate(uint16_t* mask, uint32_t width, uint32_t height)
}
// Dilate masks by one 4-connected pixel
-void dilate(uint16_t** mask, uint32_t width, uint32_t height)
+void dilate(ImageMaskData_t** mask, uint32_t width, uint32_t height)
{
- uint16_t *new_mask = _dilate(*mask, width, height);
+ ImageMaskData_t *new_mask = _dilate(*mask, width, height);
if (new_mask != NULL) {
free(*mask);
*mask = new_mask;
@@ -112,10 +112,10 @@ void dilate(uint16_t** mask, uint32_t width, uint32_t height)
}
// Erode masks by one 4-connected pixel
-uint16_t* _erode(uint16_t* mask, uint32_t width, uint32_t height)
+ImageMaskData_t* _erode(ImageMaskData_t* mask, uint32_t width, uint32_t height)
{
- uint16_t *new_mask = (uint16_t*)calloc(width*height,sizeof(uint16_t));
- memcpy(new_mask, mask, width*height*sizeof(uint16_t));
+ ImageMaskData_t *new_mask = (ImageMaskData_t*)calloc(width*height,sizeof(ImageMaskData_t));
+ memcpy(new_mask, mask, width*height*sizeof(ImageMaskData_t));
for (size_t y = 0; y < height; y++) {
for (size_t x = 0; x < width; x++) {
size_t current_position = xy_to_coord(x, y, width, height);
@@ -154,9 +154,9 @@ uint16_t* _erode(uint16_t* mask, uint32_t width, uint32_t height)
}
// Erode masks by one 4-connected pixel
-void erode(uint16_t** mask, uint32_t width, uint32_t height)
+void erode(ImageMaskData_t** mask, uint32_t width, uint32_t height)
{
- uint16_t *new_mask = _erode(*mask, width, height);
+ ImageMaskData_t *new_mask = _erode(*mask, width, height);
if (new_mask != NULL) {
free(*mask);
*mask = new_mask;
@@ -164,10 +164,10 @@ void erode(uint16_t** mask, uint32_t width, uint32_t height)
}
// Close up masks by N-pixels
-uint16_t* _closeup(uint16_t* mask, uint32_t width, uint32_t height, size_t num_pixels)
+ImageMaskData_t* _closeup(ImageMaskData_t* mask, uint32_t width, uint32_t height, size_t num_pixels)
{
- uint16_t *new_mask = (uint16_t*)calloc(width*height,sizeof(uint16_t));
- memcpy(new_mask, mask, width*height*sizeof(uint16_t));
+ ImageMaskData_t *new_mask = (ImageMaskData_t*)calloc(width*height,sizeof(ImageMaskData_t));
+ memcpy(new_mask, mask, width*height*sizeof(ImageMaskData_t));
for (size_t count = 0; count < num_pixels; count++) {
dilate(&new_mask, width, height);
}
@@ -190,9 +190,9 @@ uint16_t* _closeup(uint16_t* mask, uint32_t width, uint32_t height, size_t num_p
// Close up masks by N-pixels
// Update pointer
-void closeup(uint16_t** mask, uint32_t width, uint32_t height, size_t num_pixels)
+void closeup(ImageMaskData_t** mask, uint32_t width, uint32_t height, size_t num_pixels)
{
- uint16_t *new_mask = _closeup(*mask, width, height, num_pixels);
+ ImageMaskData_t *new_mask = _closeup(*mask, width, height, num_pixels);
if (new_mask != NULL) {
free(*mask);
*mask = new_mask;
@@ -202,10 +202,10 @@ void closeup(uint16_t** mask, uint32_t width, uint32_t height, 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
-uint16_t* combine_masks(uint16_t *destination, uint16_t *extra_labels, uint32_t width, uint32_t height)
+ImageMaskData_t* combine_masks(ImageMaskData_t *destination, ImageMaskData_t *extra_labels, uint32_t width, uint32_t height)
{
if (destination == NULL) {
- destination = (uint16_t*)calloc(width*height, sizeof(uint16_t));
+ destination = (ImageMaskData_t*)calloc(width*height, sizeof(ImageMaskData_t));
}
for (size_t y = 0; y < height; y++) {
for (size_t x = 0; x < width; x++) {
@@ -221,7 +221,7 @@ uint16_t* combine_masks(uint16_t *destination, uint16_t *extra_labels, uint32_t
// 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
-uint16_t* tif_to_labels(char* tif_file_name, uint32_t *width, uint32_t *height, uint16_t *starting_label_p)
+ImageMaskData_t* tif_to_labels(char* tif_file_name, uint32_t *width, uint32_t *height, ImageMaskData_t *starting_label_p)
{
TIFFSetWarningHandler(TiffDummyHandler);
//-TIFF-IMAGE-OPEN-------------------------------
@@ -245,13 +245,13 @@ uint16_t* tif_to_labels(char* tif_file_name, uint32_t *width, uint32_t *height,
}
//-TIFF-LOAD-DATA--------------------------------
- void* buffer = malloc(STRIP_LENGTH*sizeof(uint8_t));
+ void* buffer = malloc(STRIP_LENGTH*sizeof(ImageData_t));
if (buffer == NULL) {
fprintf(stderr, "Memory allocation error\n");
TIFFClose(tif);
return NULL;
}
- uint8_t* image_data = calloc((*width)*(*height)*channels,sizeof(uint8_t));
+ ImageData_t* image_data = calloc((*width)*(*height)*channels,sizeof(ImageData_t));
if (image_data == NULL) {
fprintf(stderr, "Memory allocation error\n");
free(buffer);
@@ -262,15 +262,15 @@ uint16_t* tif_to_labels(char* tif_file_name, uint32_t *width, uint32_t *height,
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_data[x + y*STRIP_LENGTH] = ((uint8_t*)buffer)[x];
+ image_data[x + y*STRIP_LENGTH] = ((ImageData_t*)buffer)[x];
}
}
free(buffer);
//-FLOOD-FILL-SEGMENTATION-----------------------
//-CONTIGUOUS-REGION-FINDING---------------------
- uint16_t *labels = NULL;
- labels = (uint16_t*)calloc((*width)*(*height),sizeof(uint16_t));
+ ImageMaskData_t *labels = NULL;
+ labels = (ImageMaskData_t*)calloc((*width)*(*height),sizeof(ImageMaskData_t));
if (labels == NULL) {
fprintf(stderr, "Memory allocation error\n");
free(image_data);
@@ -293,7 +293,7 @@ uint16_t* tif_to_labels(char* tif_file_name, uint32_t *width, uint32_t *height,
}
// Convert mask to bitmap
-struct bitmap_t* uint16_to_bitmap(uint16_t* buffer, uint32_t width, uint32_t height)
+struct bitmap_t* image_mask_data_to_bitmap(ImageMaskData_t* buffer, uint32_t width, uint32_t height)
{
struct pixel_t* out_buffer = (struct pixel_t*)calloc(width*height, sizeof(struct pixel_t));
if (out_buffer == NULL) {
@@ -307,9 +307,9 @@ struct bitmap_t* uint16_to_bitmap(uint16_t* buffer, uint32_t width, uint32_t hei
for (size_t y = 0; y < height; y++) {
for (size_t x = 0; x < width; x++) {
size_t coord = x + y*width;
- uint8_t red = (buffer[coord] & 0xF00) >> 4*2;
- uint8_t green = (buffer[coord] & 0x0F0) >> 4*1;
- uint8_t blue = (buffer[coord] & 0x00F) >> 4*0;
+ 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);
@@ -322,18 +322,18 @@ struct bitmap_t* uint16_to_bitmap(uint16_t* buffer, uint32_t width, uint32_t hei
}
// Reduce a mask to the contiguous regions
-uint16_t* _reduce_contiguous_regions(uint16_t* masks, uint32_t width, uint32_t height, uint16_t* total_labels)
+ImageMaskData_t* _reduce_contiguous_regions(ImageMaskData_t* masks, uint32_t width, uint32_t height, ImageMaskData_t* total_labels)
{
- uint16_t starting_label = 1;
- uint16_t* new_masks = (uint16_t*)calloc(width*height, sizeof(uint16_t));
+ ImageMaskData_t starting_label = 1;
+ ImageMaskData_t* new_masks = (ImageMaskData_t*)calloc(width*height, sizeof(ImageMaskData_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;
- uint8_t channels = 2; // uint16_t = 2*uint8_t
- if (flood((uint8_t*)masks, new_masks, width, height, channels, x, y, &(((uint8_t*)masks)[coord*channels]), starting_label)) {
+ ImageData_t channels = 2; // ImageMaskData_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++;
}
}
@@ -347,12 +347,12 @@ uint16_t* _reduce_contiguous_regions(uint16_t* masks, uint32_t width, uint32_t h
// Reduce a mask to the contiguous regions
// Automatically update pointer to contiguous mask
// Freeing previous mask
-void reduce_contiguous_regions(uint16_t** masks_p, uint32_t width, uint32_t height, uint16_t* total_labels)
+void reduce_contiguous_regions(ImageMaskData_t** masks_p, uint32_t width, uint32_t height, ImageMaskData_t* total_labels)
{
if (masks_p == NULL) {
return;
}
- uint16_t* new_masks = _reduce_contiguous_regions(*masks_p, width, height, total_labels);
+ ImageMaskData_t* new_masks = _reduce_contiguous_regions(*masks_p, width, height, total_labels);
if (new_masks != NULL) {
free(*masks_p);
*masks_p = new_masks;