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/seg/util.c | |
parent | 123464e36f2a151d820e08db7b4b426ca2b3657a (diff) |
Named Types
Diffstat (limited to 'lib/seg/util.c')
-rw-r--r-- | lib/seg/util.c | 72 |
1 files changed, 36 insertions, 36 deletions
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; |