From 1eee6ef23be665066b4b71d221fd64e9fbd2350d Mon Sep 17 00:00:00 2001 From: Christian C Date: Tue, 4 Mar 2025 22:37:43 -0800 Subject: Streamline TIF processing --- src/main.c | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 128 insertions(+), 12 deletions(-) diff --git a/src/main.c b/src/main.c index c481df0..dddba8d 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,7 @@ #include #include #include +#include #include @@ -17,24 +18,125 @@ #define OFFSET 16 #define ONE_ROW 1 -int main(int argc, char** argv) +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; +} + +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; +} + +void combine_masks(uint16_t *destination, uint16_t *extra_labels, size_t width, size_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]; + } + } + } +} + +uint16_t* tif_to_labels(char* tif_file_name, size_t *width, size_t *height) { //----------------------------------------------- - //-LIST-FILES-IN-DIRECTORY----------------------- + //-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; + } + //----------------------------------------------- + //----------------------------------------------- - char** file_list = list_directory("data/"); - if (file_list) { - size_t index = 0; - while (1) { - char* fname = file_list[index]; - if (fname == NULL) { - break; + //-TIFF-LOAD-DATA-------------------------------- + //----------------------------------------------- + void* buffer = malloc(STRIP_LENGTH*sizeof(uint8_t)); + uint8_t* image_data = calloc((*width)*(*height)*channels,sizeof(uint8_t)); + if (image_data == NULL) { + fprintf(stderr, "Memory allocation error\n"); + 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 starting_label = 1; + 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)) { + starting_label += 1; } - printf("%s\n", fname); - free(file_list[index++]); } - free(file_list); } + free(image_data); + TIFFClose(tif); + return labels; +} + +int main(int argc, char** argv) +{ + //----------------------------------------------- + //-LIST-FILES-IN-DIRECTORY----------------------- + //----------------------------------------------- + char** file_list = NULL; + uint16_t *masks = NULL; if (argc > 1) { if (is_directory(argv[1])) { file_list = list_directory(argv[1]); @@ -45,6 +147,19 @@ int main(int argc, char** argv) if (fname == NULL) { break; } + if (is_tif_ext(fname) == FALSE) { + free(file_list[index++]); + continue; + } else { + printf("Tiff found!\n"); + } + char* fpath = full_path(argv[1], fname); + size_t width, height; + uint16_t *file_labels = tif_to_labels(fpath, &width, &height); + combine_masks(masks, file_labels, width, height); + free(file_labels); + printf("%s\n", fpath); + free(fpath); printf("%s\n", fname); free(file_list[index++]); } @@ -52,6 +167,7 @@ int main(int argc, char** argv) } } } + free(masks); //----------------------------------------------- //----------------------------------------------- -- cgit v1.2.1