diff options
-rw-r--r-- | include/lib/dir.h | 6 | ||||
-rw-r--r-- | include/lib/seg/util.h | 23 | ||||
-rw-r--r-- | src/lib/dir.c | 22 | ||||
-rw-r--r-- | src/lib/seg/util.c | 149 | ||||
-rw-r--r-- | src/main.c | 183 |
5 files changed, 208 insertions, 175 deletions
diff --git a/include/lib/dir.h b/include/lib/dir.h index 39fe435..0ec9cac 100644 --- a/include/lib/dir.h +++ b/include/lib/dir.h @@ -10,4 +10,10 @@ bool_t is_directory(char* dirname); // List directory char** list_directory(char* dirname); +// Get full path +char* full_path(char* dir, char* file); + +// Determines if file name has tif file extension +bool_t is_tif_ext(char* file_name); + #endif diff --git a/include/lib/seg/util.h b/include/lib/seg/util.h new file mode 100644 index 0000000..1fd0247 --- /dev/null +++ b/include/lib/seg/util.h @@ -0,0 +1,23 @@ +#ifndef INC_LIB_SEG_UTIL_H +#define INC_LIB_SEG_UTIL_H + +#include <stdint.h> +#include <stddef.h> + +// Convert x,y coords to linear coordinate +size_t xy_to_coord(size_t x, size_t y, uint32_t width, uint32_t height); + +// Dilate masks by one 4-connected pixel +uint16_t* dilate(uint16_t* mask, uint32_t width, uint32_t height); + +// 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); + +// 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); + +#endif diff --git a/src/lib/dir.c b/src/lib/dir.c index a681d26..be2a2ac 100644 --- a/src/lib/dir.c +++ b/src/lib/dir.c @@ -45,3 +45,25 @@ char** list_directory(char* dirname) { return NULL; } +// Get full path +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[dir_len] = '/'; + return fpath; +} + +// Determines if file name has tif file extension +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')) { + return TRUE; + } + return FALSE; +} diff --git a/src/lib/seg/util.c b/src/lib/seg/util.c new file mode 100644 index 0000000..d7b91b1 --- /dev/null +++ b/src/lib/seg/util.c @@ -0,0 +1,149 @@ +#include <lib/seg/util.h> +#include <lib/algo/flood_fill.h> +#include <tiffio.h> +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> + +// Convert x,y coords to linear coordinate +size_t xy_to_coord(size_t x, size_t y, uint32_t width, uint32_t height) +{ + return x + y*width; +} + +// Dilate masks by one 4-connected pixel +uint16_t* dilate(uint16_t* mask, uint32_t width, uint32_t height) +{ + uint16_t *new_mask = (uint16_t*)calloc(width*height,sizeof(uint16_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); + if (mask[current_position] != 0) { + new_mask[current_position] = mask[current_position]; + continue; + } + size_t proposed_position; + if (x != 0) { + proposed_position = xy_to_coord(x-1, y, width, height); + if (mask[proposed_position] != 0) { + new_mask[current_position] = mask[proposed_position]; + continue; + } + } + if ((x+1) != width) { + proposed_position = xy_to_coord(x+1, y, width, height); + if (mask[proposed_position] != 0) { + new_mask[current_position] = mask[proposed_position]; + continue; + } + } + if (y != 0) { + proposed_position = xy_to_coord(x, y-1, width, height); + if (mask[proposed_position] != 0) { + new_mask[current_position] = mask[proposed_position]; + continue; + } + } + if ((y+1) != height) { + proposed_position = xy_to_coord(x, y+1, width, height); + if (mask[proposed_position] != 0) { + new_mask[current_position] = mask[proposed_position]; + continue; + } + } + } + } + return new_mask; +} + +// 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) +{ + if (destination == NULL) { + destination = (uint16_t*)calloc(width*height, sizeof(uint16_t)); + } + for (size_t y = 0; y < height; y++) { + for (size_t x = 0; x < width; x++) { + size_t coord = x + y*width; + if (destination[coord] == 0) { + destination[coord] = extra_labels[coord]; + } + } + } + return destination; +} + +// 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) +{ + //-TIFF-IMAGE-OPEN------------------------------- + TIFF *tif = TIFFOpen(tif_file_name, "r"); + if (!tif) { + fprintf(stderr, "Failed to open TIFF file\n"); + return NULL; + } + + //-TIFF-FIND-DIMENSIONS-------------------------- + size_t channels = 1; + TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, width); + TIFFGetField(tif, TIFFTAG_IMAGELENGTH, height); + + tmsize_t STRIP_LENGTH = TIFFStripSize(tif); + tmsize_t STRIP_COUNT = TIFFNumberOfStrips(tif); + if ((*width)*(*height)*3 == STRIP_LENGTH*STRIP_COUNT) { + channels = 3; + } else if ((*width)*(*height)*4 == STRIP_LENGTH*STRIP_COUNT) { + channels = 4; + } + + //-TIFF-LOAD-DATA-------------------------------- + void* buffer = malloc(STRIP_LENGTH*sizeof(uint8_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)); + if (image_data == NULL) { + fprintf(stderr, "Memory allocation error\n"); + free(buffer); + TIFFClose(tif); + return NULL; + } + for (size_t y = 0; y < STRIP_COUNT; y++) { + 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]; + } + } + free(buffer); + + //-FLOOD-FILL-SEGMENTATION----------------------- + //-CONTIGUOUS-REGION-FINDING--------------------- + uint16_t *labels = NULL; + labels = (uint16_t*)calloc((*width)*(*height),sizeof(uint16_t)); + if (labels == NULL) { + fprintf(stderr, "Memory allocation error\n"); + free(image_data); + TIFFClose(tif); + return NULL; + } + // Flood fill on each pixel + // Increase label for each success + for (size_t y = 0; y < *height; y++) { + for (size_t x = 0; x < *width; x++) { + size_t coord = x + y*(*width); + if(flood(image_data, labels, *width, *height, channels, x, y, &(image_data[coord*channels]), *starting_label_p)) { + *starting_label_p += 1; + } + } + } + free(image_data); + TIFFClose(tif); + return labels; +} @@ -1,187 +1,18 @@ #include <stdio.h> #include <stdlib.h> -#include <assert.h> -#include <string.h> #include <raylib.h> -#include <tiffio.h> - #include <lib/lib.h> #include <lib/bool.h> #include <lib/dir.h> #include <lib/time.h> #include <lib/color.h> #include <lib/algo/flood_fill.h> +#include <lib/seg/util.h> #define OFFSET 16 -// Get full path -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[dir_len] = '/'; - return fpath; -} - -// Determines if file name has tif file extension -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')) { - return TRUE; - } - return FALSE; -} - -// Convert x,y coords to linear coordinate -size_t xy_to_coord(size_t x, size_t y, uint32_t width, uint32_t height) -{ - return x + y*width; -} - -// Dilate masks by one 4-connected pixel -uint16_t* dilate(uint16_t* mask, uint32_t width, uint32_t height) -{ - uint16_t *new_mask = (uint16_t*)calloc(width*height,sizeof(uint16_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); - if (mask[current_position] != 0) { - new_mask[current_position] = mask[current_position]; - continue; - } - size_t proposed_position; - if (x != 0) { - proposed_position = xy_to_coord(x-1, y, width, height); - if (mask[proposed_position] != 0) { - new_mask[current_position] = mask[proposed_position]; - continue; - } - } - if ((x+1) != width) { - proposed_position = xy_to_coord(x+1, y, width, height); - if (mask[proposed_position] != 0) { - new_mask[current_position] = mask[proposed_position]; - continue; - } - } - if (y != 0) { - proposed_position = xy_to_coord(x, y-1, width, height); - if (mask[proposed_position] != 0) { - new_mask[current_position] = mask[proposed_position]; - continue; - } - } - if ((y+1) != height) { - proposed_position = xy_to_coord(x, y+1, width, height); - if (mask[proposed_position] != 0) { - new_mask[current_position] = mask[proposed_position]; - continue; - } - } - } - } - return new_mask; -} - -// 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) -{ - if (destination == NULL) { - destination = (uint16_t*)calloc(width*height, sizeof(uint16_t)); - } - for (size_t y = 0; y < height; y++) { - for (size_t x = 0; x < width; x++) { - size_t coord = x + y*width; - if (destination[coord] == 0) { - destination[coord] = extra_labels[coord]; - } - } - } - return destination; -} - -// 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) -{ - //-TIFF-IMAGE-OPEN------------------------------- - TIFF *tif = TIFFOpen(tif_file_name, "r"); - if (!tif) { - fprintf(stderr, "Failed to open TIFF file\n"); - return NULL; - } - - //-TIFF-FIND-DIMENSIONS-------------------------- - size_t channels = 1; - TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, width); - TIFFGetField(tif, TIFFTAG_IMAGELENGTH, height); - - tmsize_t STRIP_LENGTH = TIFFStripSize(tif); - tmsize_t STRIP_COUNT = TIFFNumberOfStrips(tif); - if ((*width)*(*height)*3 == STRIP_LENGTH*STRIP_COUNT) { - channels = 3; - } else if ((*width)*(*height)*4 == STRIP_LENGTH*STRIP_COUNT) { - channels = 4; - } - - //-TIFF-LOAD-DATA-------------------------------- - void* buffer = malloc(STRIP_LENGTH*sizeof(uint8_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)); - if (image_data == NULL) { - fprintf(stderr, "Memory allocation error\n"); - free(buffer); - TIFFClose(tif); - return NULL; - } - for (size_t y = 0; y < STRIP_COUNT; y++) { - 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]; - } - } - free(buffer); - - //-FLOOD-FILL-SEGMENTATION----------------------- - //-CONTIGUOUS-REGION-FINDING--------------------- - uint16_t *labels = NULL; - labels = (uint16_t*)calloc((*width)*(*height),sizeof(uint16_t)); - if (labels == NULL) { - fprintf(stderr, "Memory allocation error\n"); - free(image_data); - TIFFClose(tif); - return NULL; - } - // Flood fill on each pixel - // Increase label for each success - for (size_t y = 0; y < *height; y++) { - for (size_t x = 0; x < *width; x++) { - size_t coord = x + y*(*width); - if(flood(image_data, labels, *width, *height, channels, x, y, &(image_data[coord*channels]), *starting_label_p)) { - *starting_label_p += 1; - } - } - } - free(image_data); - TIFFClose(tif); - return labels; -} - int main(int argc, char** argv) { //----------------------------------------------- @@ -204,23 +35,25 @@ int main(int argc, char** argv) if (is_tif_ext(fname) == FALSE) { free(file_list[index++]); continue; - } else { - printf("Tiff found!\n"); } char* fpath = full_path(argv[1], fname); + printf("Loading %s...\n", fpath); uint16_t *file_labels = tif_to_labels(fpath, &width, &height, &starting_label); masks = combine_masks(masks, file_labels, width, height); free(file_labels); - printf("%s\n", fpath); free(fpath); - printf("%s\n", fname); free(file_list[index++]); } free(file_list); } } } - printf("%u %u\n", width, height); + if (masks == NULL) { + fprintf(stderr, "No masks found!\n"); + return 1; + } + printf("%u labels found\n", starting_label-1); + printf("Mask dimensions: %u %u\n", width, height); for (uint16_t count = 0; count < 3; count++) { uint16_t *new_labels = dilate(masks, width, height); free(masks); |