From b12c8440da356cbf4deb91f9598d84852e9d8dce Mon Sep 17 00:00:00 2001
From: Christian C <cc@localhost>
Date: Sat, 22 Mar 2025 23:03:04 -0700
Subject: Partial Conversion #1

---
 lib/seg/util.c | 43 +++++++++++++++++++++++++++++++++----------
 1 file changed, 33 insertions(+), 10 deletions(-)

(limited to 'lib/seg')

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
-- 
cgit v1.2.1