diff options
author | Christian C <cc@localhost> | 2025-03-22 22:27:40 -0700 |
---|---|---|
committer | Christian C <cc@localhost> | 2025-03-22 22:27:40 -0700 |
commit | 16422831ed49c573c1b8a43ba907bceb00fd5eea (patch) | |
tree | 1c2d86d5eecf6d1b7b4951f0b20f8c86e6939ac2 /lib | |
parent | 123464e36f2a151d820e08db7b4b426ca2b3657a (diff) |
Named Types
Diffstat (limited to 'lib')
-rw-r--r-- | lib/algo/avl_tree.c | 4 | ||||
-rw-r--r-- | lib/algo/flood_fill.c | 2 | ||||
-rw-r--r-- | lib/color.c | 6 | ||||
-rw-r--r-- | lib/png.c | 6 | ||||
-rw-r--r-- | lib/seg/mask_data.c | 44 | ||||
-rw-r--r-- | lib/seg/util.c | 72 |
6 files changed, 67 insertions, 67 deletions
diff --git a/lib/algo/avl_tree.c b/lib/algo/avl_tree.c index 9d38662..3b5c8aa 100644 --- a/lib/algo/avl_tree.c +++ b/lib/algo/avl_tree.c @@ -5,7 +5,7 @@ #include <stdio.h> // Get the height of an AVL node -uint8_t get_height(struct AVLNode* node) +AvlHeight_t get_height(struct AVLNode* node) { if (node == NULL) { return 0; @@ -14,7 +14,7 @@ uint8_t get_height(struct AVLNode* node) } // Get the Maximum Height between two -uint8_t max_height(uint8_t a, uint8_t b) +AvlHeight_t max_height(AvlHeight_t a, AvlHeight_t b) { return (a > b) ? a : b; } diff --git a/lib/algo/flood_fill.c b/lib/algo/flood_fill.c index 62db658..0506532 100644 --- a/lib/algo/flood_fill.c +++ b/lib/algo/flood_fill.c @@ -8,7 +8,7 @@ // 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(uint8_t* image, uint16_t* mask, size_t width, size_t height, size_t channels, size_t x, size_t y, uint8_t* fill_color, uint16_t label) +bool_t flood(ImageData_t* image, ImageMaskData_t* mask, size_t width, size_t height, size_t channels, size_t x, size_t y, ImageData_t* fill_color, ImageMaskData_t label) { if ((x >= width) | (y >= height)) { return FALSE; diff --git a/lib/color.c b/lib/color.c index a0a50fd..172b627 100644 --- a/lib/color.c +++ b/lib/color.c @@ -4,7 +4,7 @@ // Color Equal to Background // Background: zeros in RGB, alpha can be whatever -bool_t color_zero(const uint8_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,7 @@ bool_t color_zero(const uint8_t* color1, size_t channels) { // Color Equality // Determine if two colors are identical -bool_t color_equal(const uint8_t* color1, const uint8_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 +28,7 @@ bool_t color_equal(const uint8_t* color1, const uint8_t* color2, size_t channels } // Print out the `channels` length color vector -void print_color(uint8_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]); } @@ -9,8 +9,8 @@ void save_png(struct bitmap_t* bitmap, char* fname) FILE *fp; png_structp png_ptr = NULL; png_infop info_ptr = NULL; - uint8_t pixel_size = 3; - uint8_t pixel_depth = 8; + PixelSize_t pixel_size = 3; + PixelDepth_t pixel_depth = 8; png_byte ** row_pointers = NULL; size_t x, y; fp = fopen(fname, "wb"); @@ -43,7 +43,7 @@ void save_png(struct bitmap_t* bitmap, char* fname) 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(uint8_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; 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; |