aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/lib/data/image_types.h5
-rw-r--r--include/lib/seg/mask_data.h4
-rw-r--r--include/lib/seg/util.h15
-rw-r--r--lib/data/image_types.c48
-rw-r--r--lib/seg/mask_data.c24
-rw-r--r--lib/seg/util.c233
-rw-r--r--src/prog.c42
7 files changed, 190 insertions, 181 deletions
diff --git a/include/lib/data/image_types.h b/include/lib/data/image_types.h
index 4e226ea..8df5feb 100644
--- a/include/lib/data/image_types.h
+++ b/include/lib/data/image_types.h
@@ -5,13 +5,14 @@
#include <stddef.h>
#define ImageData_t uint8_t
-#define Image_t ImageData_t**
+#define Image_t ImageData_t***
#define MaskData_t uint16_t
#define Mask_t MaskData_t**
typedef struct Image {
size_t width;
size_t height;
+ size_t depth;
Image_t image;
} Image;
@@ -21,7 +22,7 @@ typedef struct Mask {
Mask_t image;
} Mask;
-Image* create_image(size_t width, size_t height);
+Image* create_image(size_t width, size_t height, size_t depth);
Mask* create_image_mask(size_t width, size_t height);
void free_image(Image* image);
diff --git a/include/lib/seg/mask_data.h b/include/lib/seg/mask_data.h
index 72bd5f7..70bacfa 100644
--- a/include/lib/seg/mask_data.h
+++ b/include/lib/seg/mask_data.h
@@ -58,9 +58,9 @@ bool_t in_image_mask_data_t_tree(AVLNode* root, MaskData_t value);
AVLNode* get_small_labels(AVLNode* removal_tree, AVLNode* label_tree, size_t min_area, size_t min_perimeter);
// Get mask label data
-AVLNode* get_mask_data(MaskData_t* masks, uint32_t width, uint32_t height);
+AVLNode* get_mask_data(Mask* mask);
// Filter out small masks in mask
-void filter_small_masks(MaskData_t* masks, uint32_t width, uint32_t height, size_t min_area, size_t min_perimeter);
+void filter_small_masks(Mask* mask, size_t min_area, size_t min_perimeter);
#endif
diff --git a/include/lib/seg/util.h b/include/lib/seg/util.h
index 4d6b6e4..78f948b 100644
--- a/include/lib/seg/util.h
+++ b/include/lib/seg/util.h
@@ -7,21 +7,18 @@
#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);
-
// Determine if coordinate is on a mask boundary
// Assumes mask is (WxH)
-bool_t is_on_mask_boundary(MaskData_t* mask, uint32_t width, uint32_t height, size_t x, size_t y);
+bool_t is_on_mask_boundary(Mask* mask, size_t x, size_t y);
// Dilate masks by one 4-connected pixel
-void dilate(MaskData_t** mask, uint32_t width, uint32_t height);
+void dilate(Mask* mask);
// Erode masks by one 4-connected pixel
-void erode(MaskData_t** mask, uint32_t width, uint32_t height);
+void erode(Mask* mask);
// Close up masks by N-pixels
-void closeup(MaskData_t** mask, uint32_t width, uint32_t height, size_t count);
+void closeup(Mask* mask, size_t count);
// Combine Label Masks
// For all empty spaces in the destination, put the extra label if it exists
@@ -31,7 +28,7 @@ Mask* combine_masks(Mask *destination, Mask *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
-Mask* tif_to_labels(char* tif_file_name, uint32_t *width, uint32_t *height, MaskData_t *starting_label_p);
+Mask* tif_to_labels(char* tif_file_name, MaskData_t *starting_label_p);
// Convert mask to bitmap
Bitmap* image_mask_data_to_bitmap(MaskData_t* buffer, uint32_t width, uint32_t height);
@@ -39,6 +36,6 @@ Bitmap* image_mask_data_to_bitmap(MaskData_t* buffer, uint32_t width, uint32_t h
// Reduce a mask to the contiguous regions
// Automatically update pointer to contiguous mask
// Freeing previous mask
-void reduce_contiguous_regions(MaskData_t** masks_p, uint32_t width, uint32_t height, MaskData_t* total_labels);
+void reduce_contiguous_regions(Mask* mask, MaskData_t* total_labels);
#endif
diff --git a/lib/data/image_types.c b/lib/data/image_types.c
index d58b8c6..0e40ad1 100644
--- a/lib/data/image_types.c
+++ b/lib/data/image_types.c
@@ -1,15 +1,43 @@
#include <lib/data/image_types.h>
#include <stdlib.h>
+#include <stdio.h>
-Image* create_image(size_t width, size_t height)
+Image* create_image(size_t width, size_t height, size_t channels)
{
Image* ip = (Image*)malloc(sizeof(Image));
+ if (ip == NULL) {
+ return NULL;
+ }
ip->width = width;
ip->height = height;
- ip->image = (ImageData_t**)malloc(sizeof(ImageData_t*) * ip->height);
- ImageData_t* image_data = calloc(width*height, sizeof(ImageData_t));
+ ip->depth = channels;
+ ip->image = (ImageData_t ***)malloc(sizeof(ImageData_t **) * ip->height);
+ if (ip->image == NULL) {
+ fprintf(stderr, "Memory allocation error\n");
+ free(ip);
+ return NULL;
+ }
+ ImageData_t *image_data = calloc(width * height*channels, sizeof(ImageData_t));
+ if (image_data == NULL) {
+ fprintf(stderr, "Memory allocation error\n");
+ free(ip->image);
+ free(ip);
+ return NULL;
+ }
for (size_t y = 0; y < height; y++) {
- ip->image[y] = &image_data[y*width];
+ ip->image[y] = (ImageData_t **)malloc(sizeof(ImageData_t *) * ip->width);
+ if (ip->image[y] == NULL) {
+ fprintf(stderr, "Memory allocation error\n");
+ for (size_t ty = 0; ty < y; ty++) {
+ free(ip->image[ty]);
+ }
+ free(image_data);
+ free(ip->image);
+ free(ip);
+ }
+ for (size_t x = 0; x < width; x++) {
+ ip->image[y][x] = &image_data[(y * width + x)*channels];
+ }
}
return ip;
}
@@ -17,10 +45,22 @@ Image* create_image(size_t width, size_t height)
Mask* create_image_mask(size_t width, size_t height)
{
Mask* ip = (Mask*)malloc(sizeof(Mask));
+ if (ip == NULL) {
+ return NULL;
+ }
ip->width = width;
ip->height = height;
ip->image = (MaskData_t**)malloc(sizeof(MaskData_t*) * ip->height);
+ if (ip->image == NULL) {
+ free(ip);
+ return NULL;
+ }
MaskData_t* image_data = calloc(width*height, sizeof(MaskData_t));
+ if (image_data == NULL) {
+ free(ip->image);
+ free(ip);
+ return NULL;
+ }
for (size_t y = 0; y < height; y++) {
ip->image[y] = &image_data[y*width];
}
diff --git a/lib/seg/mask_data.c b/lib/seg/mask_data.c
index 97e9165..6033c3b 100644
--- a/lib/seg/mask_data.c
+++ b/lib/seg/mask_data.c
@@ -227,16 +227,17 @@ AVLNode* get_small_labels(AVLNode* removal_tree, AVLNode* label_tree, size_t min
}
// Get mask label data
-AVLNode* get_mask_data(MaskData_t* masks, uint32_t width, uint32_t height)
+AVLNode *get_mask_data(Mask *mask)
{
+ uint32_t width = mask->width;
+ uint32_t height = mask->height;
AVLNode* root = NULL;
for (size_t y = 0; y < height; y++) {
for (size_t x = 0; x < width; x++) {
- size_t coord = x + y*width;
- if (masks[coord] != 0) {
- root = increase_label_area_alloc(root, masks[coord]);
- if (is_on_mask_boundary(masks, width, height, x, y)) {
- increase_label_perimeter(root, masks[coord]);
+ if (mask->image[y][x] != 0) {
+ root = increase_label_area_alloc(root, mask->image[y][x]);
+ if (is_on_mask_boundary(mask, x, y)) {
+ increase_label_perimeter(root, mask->image[y][x]);
}
}
}
@@ -245,16 +246,17 @@ AVLNode* get_mask_data(MaskData_t* masks, uint32_t width, uint32_t height)
}
// Filter out small masks in mask
-void filter_small_masks(MaskData_t* masks, uint32_t width, uint32_t height, size_t min_area, size_t min_perimeter)
+void filter_small_masks(Mask *mask, size_t min_area, size_t min_perimeter)
{
- AVLNode* root = get_mask_data(masks, width, height);
+ uint32_t width = mask->width;
+ uint32_t height = mask->height;
+ AVLNode* root = get_mask_data(mask);
AVLNode* small_label_tree = NULL;
small_label_tree = get_small_labels(NULL, root, min_area, min_perimeter);
for (size_t y = 0; y < height; y++) {
for (size_t x = 0; x < width; x++) {
- size_t coord = x + y*width;
- if (in_image_mask_data_t_tree(small_label_tree, masks[coord])) {
- masks[coord] = 0;
+ if (in_image_mask_data_t_tree(small_label_tree, mask->image[y][x])) {
+ mask->image[y][x] = 0;
}
}
}
diff --git a/lib/seg/util.c b/lib/seg/util.c
index 844d4a1..bc2f837 100644
--- a/lib/seg/util.c
+++ b/lib/seg/util.c
@@ -13,43 +13,31 @@ void TiffDummyHandler(const char* module, const char* fmt, va_list ap)
// ignore errors and warnings (or handle them your own way)
}
-// 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;
-}
-
// Determine if coordinate is on a mask boundary
// Assumes mask is (WxH)
-bool_t is_on_mask_boundary(MaskData_t* mask, uint32_t width, uint32_t height, size_t x, size_t y)
+bool_t is_on_mask_boundary(Mask* mask, size_t x, size_t y)
{
- size_t starting_coord = xy_to_coord(x, y, width, height);
- size_t proposed_position;
- MaskData_t current_value = mask[starting_coord];
+ MaskData_t current_value = mask->image[y][x];
// Left neighbor
if (x != 0) {
- proposed_position = xy_to_coord(x-1, y, width, height);
- if (mask[proposed_position] != current_value) {
+ if (mask->image[y][x-1] != current_value) {
return TRUE;
}
}
// Right neighbor
- if ((x+1) != width) {
- proposed_position = xy_to_coord(x+1, y, width, height);
- if (mask[proposed_position] != current_value) {
+ if ((x+1) != mask->width) {
+ if (mask->image[y][x+1] != current_value) {
return TRUE;
}
}
if (y != 0) {
- proposed_position = xy_to_coord(x, y-1, width, height);
- if (mask[proposed_position] != current_value) {
+ if (mask->image[y-1][x] != current_value) {
return TRUE;
}
}
- if ((y+1) != height) {
- proposed_position = xy_to_coord(x, y+1, width, height);
- if (mask[proposed_position] != current_value) {
+ if ((y+1) != mask->height) {
+ if (mask->image[y+1][x] != current_value) {
return TRUE;
}
}
@@ -57,145 +45,151 @@ bool_t is_on_mask_boundary(MaskData_t* mask, uint32_t width, uint32_t height, si
}
// Dilate masks by one 4-connected pixel
-MaskData_t* _dilate(MaskData_t* mask, uint32_t width, uint32_t height)
+MaskData_t* _dilate(const Mask* mask)
{
- MaskData_t *new_mask = (MaskData_t*)calloc(width*height,sizeof(MaskData_t));
+ uint32_t width = mask->width;
+ uint32_t height = mask->height;
+ Mask *new_mask = create_image_mask(width, height);
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];
+ if (mask->image[y][x] != 0) {
+ new_mask->image[y][x] = mask->image[y][x];
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];
+ if (mask->image[y][x-1] != 0) {
+ new_mask->image[y][x] = mask->image[y][x-1];
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];
+ if (mask->image[y][x+1] != 0) {
+ new_mask->image[y][x] = mask->image[y][x+1];
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];
+ if (mask->image[y-1][x] != 0) {
+ new_mask->image[y][x] = mask->image[y-1][x];
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];
+ if (mask->image[y+1][x] != 0) {
+ new_mask->image[y][x] = mask->image[y+1][x];
continue;
}
}
}
}
- return new_mask;
+ MaskData_t *ret_mask = new_mask->image[0];
+ free(new_mask->image);
+ free(new_mask);
+ return ret_mask;
}
// Dilate masks by one 4-connected pixel
-void dilate(MaskData_t** mask, uint32_t width, uint32_t height)
+void dilate(Mask* mask)
{
- MaskData_t *new_mask = _dilate(*mask, width, height);
+ MaskData_t *new_mask = _dilate(mask);
if (new_mask != NULL) {
- free(*mask);
- *mask = new_mask;
+ free(mask->image[0]);
+ for (size_t y = 0; y < mask->height; y++) {
+ mask->image[y] = &(new_mask[y*mask->width]);
+ }
}
}
// Erode masks by one 4-connected pixel
-MaskData_t* _erode(MaskData_t* mask, uint32_t width, uint32_t height)
+MaskData_t* _erode(const Mask* mask)
{
- MaskData_t *new_mask = (MaskData_t*)calloc(width*height,sizeof(MaskData_t));
- memcpy(new_mask, mask, width*height*sizeof(MaskData_t));
+ uint32_t width = mask->width;
+ uint32_t height = mask->height;
+ Mask *new_mask = create_image_mask(width, height);
+ memcpy(new_mask->image[0], mask->image[0], width*height*sizeof(MaskData_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);
- 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] = 0;
+ if (mask->image[y][x-1] == 0) {
+ new_mask->image[y][x] = 0;
continue;
}
}
if ((x+1) != width) {
- proposed_position = xy_to_coord(x+1, y, width, height);
- if (mask[proposed_position] == 0) {
- new_mask[current_position] = 0;
+ if (mask->image[y][x+1] == 0) {
+ new_mask->image[y][x] = 0;
continue;
}
}
if (y != 0) {
- proposed_position = xy_to_coord(x, y-1, width, height);
- if (mask[proposed_position] == 0) {
- new_mask[current_position] = 0;
+ if (mask->image[y-1][x] == 0) {
+ new_mask->image[y][x] = 0;
continue;
}
}
if ((y+1) != height) {
- proposed_position = xy_to_coord(x, y+1, width, height);
- if (mask[proposed_position] == 0) {
- new_mask[current_position] = 0;
+ if (mask->image[y+1][x] == 0) {
+ new_mask->image[y][x] = 0;
continue;
}
}
}
}
- return new_mask;
+ MaskData_t *ret_mask = new_mask->image[0];
+ free(new_mask->image);
+ free(new_mask);
+ return ret_mask;
}
// Erode masks by one 4-connected pixel
-void erode(MaskData_t** mask, uint32_t width, uint32_t height)
+void erode(Mask* mask)
{
- MaskData_t *new_mask = _erode(*mask, width, height);
+ MaskData_t *new_mask = _erode(mask);
if (new_mask != NULL) {
- free(*mask);
- *mask = new_mask;
+ free(mask->image[0]);
+ for (size_t y = 0; y < mask->height; y++) {
+ mask->image[y] = &(new_mask[y*mask->width]);
+ }
}
}
// Close up masks by N-pixels
-MaskData_t* _closeup(MaskData_t* mask, uint32_t width, uint32_t height, size_t num_pixels)
+MaskData_t* _closeup(Mask* mask, size_t num_pixels)
{
- MaskData_t *new_mask = (MaskData_t*)calloc(width*height,sizeof(MaskData_t));
- memcpy(new_mask, mask, width*height*sizeof(MaskData_t));
+ Mask *new_mask = create_image_mask(mask->width, mask->height);
+ memcpy(new_mask->image[0], mask->image[0],
+ mask->width * mask->height * sizeof(MaskData_t));
for (size_t count = 0; count < num_pixels; count++) {
- dilate(&new_mask, width, height);
+ dilate(mask);
}
for (size_t count = 0; count < num_pixels; count++) {
- erode(&new_mask, width, height);
- }
- // Retain original mask at the very least
- for (size_t y = 0; y < height; y++) {
- for (size_t x = 0; x < width; x++) {
- size_t coord = x + y*width;
- if (mask[coord] != 0) {
- if (new_mask[coord] != mask[coord]) {
- new_mask[coord] = mask[coord];
- }
+ erode(mask);
+ }
+ for (size_t y = 0; y < mask->height; y++) {
+ for (size_t x = 0; x < mask->width; x++) {
+ if (mask->image[y][x] != 0) {
+ if (new_mask->image[y][x] != mask->image[y][x]) {
+ new_mask->image[y][x] = mask->image[y][x];
+ }
}
}
}
- return new_mask;
+ MaskData_t *ret_mask = new_mask->image[0];
+ free(new_mask->image);
+ free(new_mask);
+ return ret_mask;
}
// Close up masks by N-pixels
// Update pointer
-void closeup(MaskData_t** mask, uint32_t width, uint32_t height, size_t num_pixels)
+void closeup(Mask* mask, size_t num_pixels)
{
- MaskData_t *new_mask = _closeup(*mask, width, height, num_pixels);
+ MaskData_t *new_mask = _closeup(mask, num_pixels);
if (new_mask != NULL) {
- free(*mask);
- *mask = new_mask;
+ free(mask->image[0]);
+ for (size_t y = 0; y < mask->height; y++) {
+ mask->image[y] = &(new_mask[y*mask->width]);
+ }
}
}
@@ -220,8 +214,10 @@ Mask* combine_masks(Mask *destination, Mask *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
-Mask* tif_to_labels(char* tif_file_name, uint32_t *width, uint32_t *height, MaskData_t *starting_label_p)
+Mask *tif_to_labels(char *tif_file_name, MaskData_t *starting_label_p)
{
+ uint32_t width = 0;
+ uint32_t height = 0;
TIFFSetWarningHandler(TiffDummyHandler);
//-TIFF-IMAGE-OPEN-------------------------------
TIFF *tif = TIFFOpen(tif_file_name, "r");
@@ -231,17 +227,13 @@ Mask* tif_to_labels(char* tif_file_name, uint32_t *width, uint32_t *height, Mask
}
//-TIFF-FIND-DIMENSIONS--------------------------
- size_t channels = 1;
- TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, width);
- TIFFGetField(tif, TIFFTAG_IMAGELENGTH, height);
+ size_t channels;
+ 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;
- }
+ channels = (STRIP_LENGTH*STRIP_COUNT)/(width*height);
//-TIFF-LOAD-DATA--------------------------------
void* buffer = malloc(STRIP_LENGTH*sizeof(ImageData_t));
@@ -250,8 +242,8 @@ Mask* tif_to_labels(char* tif_file_name, uint32_t *width, uint32_t *height, Mask
TIFFClose(tif);
return NULL;
}
- ImageData_t* image_data = calloc((*width)*(*height)*channels,sizeof(ImageData_t));
- if (image_data == NULL) {
+ Image *image = create_image(width, height, channels);
+ if (image == NULL) {
fprintf(stderr, "Memory allocation error\n");
free(buffer);
TIFFClose(tif);
@@ -261,55 +253,30 @@ Mask* tif_to_labels(char* tif_file_name, uint32_t *width, uint32_t *height, Mask
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] = ((ImageData_t*)buffer)[x];
+ image->image[0][0][x + y*STRIP_LENGTH] = ((ImageData_t*)buffer)[x];
}
}
free(buffer);
//-FLOOD-FILL-SEGMENTATION-----------------------
//-CONTIGUOUS-REGION-FINDING---------------------
- Mask *im_data = create_image_mask((size_t)*width, (size_t)*height);
+ Mask *im_data = create_image_mask((size_t)width, (size_t)height);
if (im_data == NULL) {
fprintf(stderr, "Memory allocation error\n");
- free(image_data);
+ free_image(image);
TIFFClose(tif);
return NULL;
}
- if (im_data->image == NULL) {
- fprintf(stderr, "Memory allocation error\n");
- free_image_mask(im_data);
- free(image_data);
- TIFFClose(tif);
- return NULL;
- }
- if (im_data->image[0] == NULL) {
- fprintf(stderr, "Memory allocation error\n");
- free_image_mask(im_data);
- free(image_data);
- TIFFClose(tif);
- return NULL;
- }
- MaskData_t *labels = im_data->image[0];
- /*
- labels = (MaskData_t*)calloc((*width)*(*height),sizeof(MaskData_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)) {
+ for (size_t y = 0; y < height; y++) {
+ for (size_t x = 0; x < width; x++) {
+ if(flood(image->image[0][0], im_data->image[0], width, height, channels, x, y, image->image[y][x], *starting_label_p)) {
*starting_label_p += 1;
}
}
}
- free(image_data);
+ free_image(image);
TIFFClose(tif);
return im_data;
//return labels;
@@ -370,14 +337,16 @@ MaskData_t* _reduce_contiguous_regions(MaskData_t* masks, uint32_t width, uint32
// Reduce a mask to the contiguous regions
// Automatically update pointer to contiguous mask
// Freeing previous mask
-void reduce_contiguous_regions(MaskData_t** masks_p, uint32_t width, uint32_t height, MaskData_t* total_labels)
+void reduce_contiguous_regions(Mask* mask, MaskData_t* total_labels)
{
- if (masks_p == NULL) {
+ if (mask == NULL) {
return;
}
- MaskData_t* new_masks = _reduce_contiguous_regions(*masks_p, width, height, total_labels);
+ MaskData_t* new_masks = _reduce_contiguous_regions(mask->image[0], mask->width, mask->height, total_labels);
if (new_masks != NULL) {
- free(*masks_p);
- *masks_p = new_masks;
+ free(mask->image[0]);
+ for (size_t y = 0; y < mask->height; y++) {
+ mask->image[y] = &new_masks[y*mask->width];
+ }
}
}
diff --git a/src/prog.c b/src/prog.c
index 3335268..0f59530 100644
--- a/src/prog.c
+++ b/src/prog.c
@@ -75,8 +75,8 @@ int main(int argc, char** argv)
break;
}
}
- if (!silent) {
- for (;optind < argc; optind++) {
+ for (;optind < argc; optind++) {
+ if (!silent) {
printf("Extra arguments: %s\n", argv[optind]);
}
}
@@ -85,9 +85,7 @@ int main(int argc, char** argv)
//-PROCESS-FILES-IN-DIRECTORY--------------------
//-----------------------------------------------
char** file_list = NULL;
- uint32_t width, height;
MaskData_t starting_label = 1;
- MaskData_t *masks = NULL;
Mask *masks_im = NULL;
// Expect a directory to be passed as the first argument
if (directory != NULL) {
@@ -118,13 +116,13 @@ int main(int argc, char** argv)
//-----------------------------------------------
//-PROCESS-TIFF-TO-LABELS------------------------
//-----------------------------------------------
- Mask *file_im = tif_to_labels(fpath, &width, &height, &starting_label);
- MaskData_t *file_labels = file_im->image[0];
- if (file_labels == NULL) {
- free(fpath);
- free(file_list[index]);
+ Mask *file_im =
+ tif_to_labels(fpath, &starting_label);
+ if (file_im == NULL) {
+ free(fpath);
+ free(file_list[index]);
continue;
- }
+ }
//-----------------------------------------------
//-COMBINE-LABELS-TO-GLOBAL-MASK-----------------
//-----------------------------------------------
@@ -137,16 +135,18 @@ int main(int argc, char** argv)
}
}
}
- masks = masks_im->image[0];
- if (masks == NULL) {
+ if (masks_im == NULL) {
fprintf(stderr, "No masks found!\n");
return 1;
}
+ uint32_t width = (uint32_t)masks_im->width;
+ uint32_t height = (uint32_t)masks_im->height;
+
//-----------------------------------------------
//-FIND-CONTIGUOUS-REGIONS-----------------------
//-----------------------------------------------
- reduce_contiguous_regions(&masks, width, height, &starting_label);
+ reduce_contiguous_regions(masks_im, &starting_label);
if (!silent) {
printf("%u labels found\n", starting_label-1);
printf("Mask dimensions: %u %u\n", width, height);
@@ -155,7 +155,7 @@ int main(int argc, char** argv)
//-FILTER-SMALL-REGIONS-OUT----------------------
//-----------------------------------------------
TIME(ts_filter_start);
- filter_small_masks(masks, width, height, min_area, min_perimeter);
+ filter_small_masks(masks_im, min_area, min_perimeter);
TIME(ts_filter_end);
if (!silent) {
printf("Removing small labels took %f ms\n", 1000*diff_time(&ts_filter_end, &ts_filter_start));
@@ -165,7 +165,7 @@ int main(int argc, char** argv)
//-----------------------------------------------
//--to-make-labels-span-1-to-n-------------------
//-----------------------------------------------
- reduce_contiguous_regions(&masks, width, height, &starting_label);
+ reduce_contiguous_regions(masks_im, &starting_label);
if (!silent) {
printf("%u remaining labels found\n", starting_label-1);
printf("Mask dimensions: %u %u\n", width, height);
@@ -176,7 +176,7 @@ int main(int argc, char** argv)
//-GET-MASK-META-INFORMATION---------------------
//-----------------------------------------------
AVLNode* root = NULL;
- root = get_mask_data(masks, width, height);
+ root = get_mask_data(masks_im);
if (!silent) {
printf("Inorder traversal of AVL tree: ");
print_label(root);
@@ -188,7 +188,7 @@ int main(int argc, char** argv)
//-CLOSE-UP-SMALL-GAPS-BETWEEN-REGIONS-----------
//-----------------------------------------------
TIME(ts_start);
- closeup(&masks, width, height, closeup_pixel_count);
+ closeup(masks_im,closeup_pixel_count);
TIME(ts_end);
if (!silent) {
printf("Closing (%lu) up took %f ms\n", closeup_pixel_count, 1000*diff_time(&ts_end, &ts_start));
@@ -200,14 +200,14 @@ int main(int argc, char** argv)
//-----------------------------------------------
//-SAVE-MASK-AS-BINARY-AND-PNG-------------------
//-----------------------------------------------
- if (masks != NULL) {
- Bitmap* bitmap = image_mask_data_to_bitmap(masks, width, height);
+ if (masks_im != NULL) {
+ Bitmap* bitmap = image_mask_data_to_bitmap(masks_im->image[0], width, height);
if (bitmap != NULL) {
save_png(bitmap, png_file);
free(bitmap);
}
- write_array(bin_file, masks, width*height*sizeof(MaskData_t));
- free(masks);
+ write_array(bin_file, masks_im->image[0], width*height*sizeof(MaskData_t));
+ free_image_mask(masks_im);
}
TIME(ts_g_end);
if (!silent) {