diff options
author | Christian C <cc@localhost> | 2025-03-22 23:03:04 -0700 |
---|---|---|
committer | Christian C <cc@localhost> | 2025-03-22 23:03:04 -0700 |
commit | b12c8440da356cbf4deb91f9598d84852e9d8dce (patch) | |
tree | 8bb8abe01244d5ef2afefc7485c1157acd14c277 | |
parent | ca1d8c3194f679d1901ea83ff954c82114d5559b (diff) |
Partial Conversion #1
-rw-r--r-- | include/lib/seg/util.h | 4 | ||||
-rw-r--r-- | lib/seg/util.c | 43 | ||||
-rw-r--r-- | src/prog.c | 9 |
3 files changed, 41 insertions, 15 deletions
diff --git a/include/lib/seg/util.h b/include/lib/seg/util.h index ef85c2c..395c5cd 100644 --- a/include/lib/seg/util.h +++ b/include/lib/seg/util.h @@ -25,12 +25,12 @@ void closeup(ImageMaskData_t** mask, uint32_t width, uint32_t height, size_t cou // Combine Label Masks // For all empty spaces in the destination, put the extra label if it exists // Allocates an array if destination is unallocated -ImageMaskData_t* combine_masks(ImageMaskData_t *destination, ImageMaskData_t *extra_labels, uint32_t width, uint32_t height); +struct ImageMask* combine_masks(struct ImageMask *destination, struct ImageMask *extra_labels); // 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 -ImageMaskData_t* tif_to_labels(char* tif_file_name, uint32_t *width, uint32_t *height, ImageMaskData_t *starting_label_p); +struct ImageMask* tif_to_labels(char* tif_file_name, uint32_t *width, uint32_t *height, ImageMaskData_t *starting_label_p); // Convert mask to bitmap struct bitmap_t* image_mask_data_to_bitmap(ImageMaskData_t* buffer, uint32_t width, uint32_t height); diff --git a/lib/seg/util.c b/lib/seg/util.c index 4290d46..2952952 100644 --- a/lib/seg/util.c +++ b/lib/seg/util.c @@ -202,16 +202,15 @@ void closeup(ImageMaskData_t** mask, uint32_t width, uint32_t height, size_t num // Combine Label Masks // For all empty spaces in the destination, put the extra label if it exists // Allocates an array if destination is unallocated -ImageMaskData_t* combine_masks(ImageMaskData_t *destination, ImageMaskData_t *extra_labels, uint32_t width, uint32_t height) +struct ImageMask* combine_masks(struct ImageMask *destination, struct ImageMask *extra_labels) { if (destination == NULL) { - destination = (ImageMaskData_t*)calloc(width*height, sizeof(ImageMaskData_t)); + destination = create_image_mask(extra_labels->width, extra_labels->height); } - 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]; + for (size_t y = 0; y < destination->height; y++) { + for (size_t x = 0; x < destination->width; x++) { + if (destination->mask[y][x] == 0) { + destination->mask[y][x] = extra_labels->mask[y][x]; } } } @@ -221,7 +220,7 @@ ImageMaskData_t* combine_masks(ImageMaskData_t *destination, ImageMaskData_t *ex // 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 -ImageMaskData_t* tif_to_labels(char* tif_file_name, uint32_t *width, uint32_t *height, ImageMaskData_t *starting_label_p) +struct ImageMask* tif_to_labels(char* tif_file_name, uint32_t *width, uint32_t *height, ImageMaskData_t *starting_label_p) { TIFFSetWarningHandler(TiffDummyHandler); //-TIFF-IMAGE-OPEN------------------------------- @@ -269,7 +268,29 @@ ImageMaskData_t* tif_to_labels(char* tif_file_name, uint32_t *width, uint32_t *h //-FLOOD-FILL-SEGMENTATION----------------------- //-CONTIGUOUS-REGION-FINDING--------------------- - ImageMaskData_t *labels = NULL; + struct ImageMask *im_data = create_image_mask((size_t)*width, (size_t)*height); + if (im_data == NULL) { + fprintf(stderr, "Memory allocation error\n"); + free(image_data); + TIFFClose(tif); + return NULL; + } + if (im_data->mask == NULL) { + fprintf(stderr, "Memory allocation error\n"); + free_image_mask(im_data); + free(image_data); + TIFFClose(tif); + return NULL; + } + if (im_data->mask[0] == NULL) { + fprintf(stderr, "Memory allocation error\n"); + free_image_mask(im_data); + free(image_data); + TIFFClose(tif); + return NULL; + } + ImageMaskData_t *labels = im_data->mask[0]; + /* labels = (ImageMaskData_t*)calloc((*width)*(*height),sizeof(ImageMaskData_t)); if (labels == NULL) { fprintf(stderr, "Memory allocation error\n"); @@ -277,6 +298,7 @@ ImageMaskData_t* tif_to_labels(char* tif_file_name, uint32_t *width, uint32_t *h TIFFClose(tif); return NULL; } + */ // Flood fill on each pixel // Increase label for each success for (size_t y = 0; y < *height; y++) { @@ -289,7 +311,8 @@ ImageMaskData_t* tif_to_labels(char* tif_file_name, uint32_t *width, uint32_t *h } free(image_data); TIFFClose(tif); - return labels; + return im_data; + //return labels; } // Convert mask to bitmap @@ -88,6 +88,7 @@ int main(int argc, char** argv) uint32_t width, height; ImageMaskData_t starting_label = 1; ImageMaskData_t *masks = NULL; + struct ImageMask *masks_im = NULL; // Expect a directory to be passed as the first argument if (directory != NULL) { // Ensure the directory exists @@ -117,7 +118,8 @@ int main(int argc, char** argv) //----------------------------------------------- //-PROCESS-TIFF-TO-LABELS------------------------ //----------------------------------------------- - ImageMaskData_t *file_labels = tif_to_labels(fpath, &width, &height, &starting_label); + struct ImageMask *file_im = tif_to_labels(fpath, &width, &height, &starting_label); + ImageMaskData_t *file_labels = file_im->mask[0]; if (file_labels == NULL) { free(fpath); free(file_list[index]); @@ -126,8 +128,8 @@ int main(int argc, char** argv) //----------------------------------------------- //-COMBINE-LABELS-TO-GLOBAL-MASK----------------- //----------------------------------------------- - masks = combine_masks(masks, file_labels, width, height); - free(file_labels); + masks_im = combine_masks(masks_im, file_im); + free_image_mask(file_im); free(fpath); free(file_list[index]); } @@ -135,6 +137,7 @@ int main(int argc, char** argv) } } } + masks = masks_im->mask[0]; if (masks == NULL) { fprintf(stderr, "No masks found!\n"); return 1; |