diff options
author | Christian C <cc@localhost> | 2025-03-04 23:01:00 -0800 |
---|---|---|
committer | Christian C <cc@localhost> | 2025-03-04 23:01:00 -0800 |
commit | 076651de2ada69bd31cc246ffe899afdc8e4cb13 (patch) | |
tree | 9ab3f98c516cee46fd3761b68983951e34d334e2 /src/main.c | |
parent | 1eee6ef23be665066b4b71d221fd64e9fbd2350d (diff) |
Combine masks
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 160 |
1 files changed, 21 insertions, 139 deletions
@@ -39,7 +39,7 @@ bool_t is_tif_ext(char* file_name) return FALSE; } -void combine_masks(uint16_t *destination, uint16_t *extra_labels, size_t width, size_t height) +uint16_t* 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)); @@ -52,9 +52,10 @@ void combine_masks(uint16_t *destination, uint16_t *extra_labels, size_t width, } } } + return destination; } -uint16_t* tif_to_labels(char* tif_file_name, size_t *width, size_t *height) +uint16_t* tif_to_labels(char* tif_file_name, uint32_t *width, uint32_t *height, uint16_t *starting_label_p) { //----------------------------------------------- //-TIFF-IMAGE-OPEN------------------------------- @@ -86,9 +87,15 @@ uint16_t* tif_to_labels(char* tif_file_name, size_t *width, size_t *height) //-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; } @@ -106,7 +113,6 @@ uint16_t* tif_to_labels(char* tif_file_name, size_t *width, size_t *height) //-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) { @@ -120,8 +126,8 @@ uint16_t* tif_to_labels(char* tif_file_name, size_t *width, size_t *height) 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; + if(flood(image_data, labels, *width, *height, channels, x, y, &(image_data[coord*channels]), *starting_label_p)) { + *starting_label_p += 1; } } } @@ -136,6 +142,8 @@ int main(int argc, char** argv) //-LIST-FILES-IN-DIRECTORY----------------------- //----------------------------------------------- char** file_list = NULL; + uint32_t width, height; + uint16_t starting_label = 1; uint16_t *masks = NULL; if (argc > 1) { if (is_directory(argv[1])) { @@ -154,9 +162,8 @@ int main(int argc, char** argv) 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); + 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); @@ -167,7 +174,8 @@ int main(int argc, char** argv) } } } - free(masks); + printf("%u %u\n", width, height); + //free(masks); //----------------------------------------------- //----------------------------------------------- @@ -177,120 +185,16 @@ int main(int argc, char** argv) SetConfigFlags(FLAG_WINDOW_RESIZABLE); const char* gui_title = "Image Manip - Useful for segmentations!"; InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, gui_title); - //----------------------------------------------- - - //----------------------------------------------- - //-TIFF-IMAGE-OPEN------------------------------- - //----------------------------------------------- - TIFF *tif = TIFFOpen("./data/test.tif", "r"); - if (!tif) { - fprintf(stderr, "Failed to open TIFF file\n"); - return 1; - } - //----------------------------------------------- - - //----------------------------------------------- - //-TIFF-FIND-DIMENSIONS-------------------------- - //----------------------------------------------- - uint32_t width, height; - 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; - } - //----------------------------------------------- - - printf("Total Strip Size: %llu\n", STRIP_LENGTH*STRIP_COUNT); - printf("Total Image Size: %lu in %hhu channel(s)\n", width*height*channels, channels); - assert(STRIP_LENGTH*STRIP_COUNT == width*height*channels); - - //----------------------------------------------- - //-TIFF-LOAD-DATA-------------------------------- - //----------------------------------------------- - void* buffer = malloc(STRIP_LENGTH*sizeof(uint8_t)); - uint8_t* raster_8 = calloc(width*height*channels,sizeof(uint8_t)); - if (raster_8 == NULL) { - fprintf(stderr, "Memory allocation error\n"); - TIFFClose(tif); - return 1; - } - 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++) { - raster_8[x + y*STRIP_LENGTH] = ((uint8_t*)buffer)[x]; - } - } - free(buffer); - //----------------------------------------------- - struct timespec ts_start, ts_end; - get_time(&ts_start); - //----------------------------------------------- - //-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(raster_8); - TIFFClose(tif); - return 1; - } - // 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(raster_8, labels, width, height, channels, x, y, &(raster_8[coord*channels]), starting_label)) { - starting_label += 1; - } - } - } // (When treating this as RGBA, last bits should ensure opaque) // This assumes 4096 (2^12) > labels for (size_t y = 0; y < height; y++) { for (size_t x = 0; x < width; x++) { - labels[x + y*width] |= 0xFF000000; + masks[x + y*width] |= 0xFF000000; } } //----------------------------------------------- - get_time(&ts_end); - printf("Time difference: %.3fms\n", 1000*diff_time(&ts_end, &ts_start)); - printf("N_labels: %u\n", starting_label-1); - - uint32_t *raster = NULL; - //----------------------------------------------- - //-READ-TIFF-IMAGE------------------------------- - //----------------------------------------------- - /* - raster = (uint32_t*)_TIFFmalloc(width*height*sizeof(uint32_t)); - if (raster == NULL) { - fprintf(stderr, "Memory allocation error\n"); - free(labels); - free(raster_8); - TIFFClose(tif); - return 1; - } - - if (!TIFFReadRGBAImage(tif, width, height, raster, 0)) { - fprintf(stderr, "Failed to read TIFF image\n"); - free(labels); - free(raster_8); - _TIFFfree(raster); - TIFFClose(tif); - return 1; - } - */ - //----------------------------------------------- //----------------------------------------------- //-RAYLIB-IMAGE-STRUCTURING---------------------- @@ -299,22 +203,8 @@ int main(int argc, char** argv) RaylibImage.width = width; RaylibImage.height = height; RaylibImage.mipmaps = 1; - // Use Loaded Image - RaylibImage.data = raster; - RaylibImage.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8; - // Use Strip Image Data - RaylibImage.data = raster_8; - if (channels == 1) { - RaylibImage.format = PIXELFORMAT_UNCOMPRESSED_GRAYSCALE; - } else if (channels == 3) { - RaylibImage.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8; - } else if (channels == 4) { - RaylibImage.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8; - } else { - assert(0); - } // Use Contiguous Labels - RaylibImage.data = labels; + RaylibImage.data = masks; RaylibImage.format = PIXELFORMAT_UNCOMPRESSED_R4G4B4A4; //----------------------------------------------- @@ -359,17 +249,9 @@ int main(int argc, char** argv) //----------------------------------------------- } - if (labels != NULL) { - free(labels); + if (masks != NULL) { + free(masks); } - if (raster_8 != NULL) { - free(raster_8); - } - if (raster != NULL) { - _TIFFfree(raster); - } - TIFFClose(tif); - CloseWindow(); return 0; } |