aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c310
1 files changed, 0 insertions, 310 deletions
diff --git a/src/main.c b/src/main.c
deleted file mode 100644
index 5bdcadb..0000000
--- a/src/main.c
+++ /dev/null
@@ -1,310 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-
-#ifdef VISUAL
-#include <raylib.h>
-#endif
-
-#include <lib/lib.h>
-#include <lib/png.h>
-#include <lib/bool.h>
-#include <lib/monad.h>
-#include <lib/dir.h>
-#include <lib/file.h>
-#include <lib/time.h>
-#include <lib/color.h>
-#include <lib/algo/flood_fill.h>
-#include <lib/algo/avl_tree.h>
-#include <lib/seg/util.h>
-#include <lib/seg/mask_data.h>
-
-#define OFFSET 16
-
-#define N_DILATIONS 10
-
-#define MIN_AREA 200
-#define MIN_PERIMETER 50
-
-int main(int argc, char** argv)
-{
- TIME(ts_g_start);
- struct AVLNode* root = NULL;
- //-----------------------------------------------
- //-LIST-FILES-IN-DIRECTORY-----------------------
- //-----------------------------------------------
- char** file_list = NULL;
- uint32_t width, height;
- uint16_t starting_label = 1;
- uint16_t *masks = NULL;
- // Expect a directory to be passed as the first argument
- if (argc > 1) {
- // Ensure the directory exists
- if (is_directory(argv[1])) {
- // List files in the ddirectory
- file_list = list_directory(argv[1]);
- if (file_list) {
- size_t index = 0;
- // For each file
- while (1) {
- char* fname = file_list[index];
- if (fname == NULL) {
- break;
- }
- if (is_tif_ext(fname) == FALSE) {
- free(file_list[index++]);
- continue;
- }
- // If we have a tiff file
- // 1. Convert to labels
- // 2. Find contiguous regions
- // 3. Combine with current total mask
- // 4. Free up allocations made in this process
- char* fpath = full_path(argv[1], fname);
- if (fpath == NULL) {
- free(file_list[index++]);
- continue;
- }
- printf("Loading %s...\n", fpath);
- uint16_t *file_labels = tif_to_labels(fpath, &width, &height, &starting_label);
- if (file_labels == NULL) {
- free(fpath);
- free(file_list[index++]);
- continue;
- }
- masks = combine_masks(masks, file_labels, width, height);
- free(file_labels);
- free(fpath);
- free(file_list[index++]);
- }
- free(file_list);
- }
- }
- }
- if (masks == NULL) {
- fprintf(stderr, "No masks found!\n");
- return 1;
- }
-
- // Regenerate contiguous labels
- starting_label = 1;
- uint16_t* labels = (uint16_t*)calloc(width*height, sizeof(uint16_t));
- if (labels == NULL) {
- fprintf(stderr, "Memory allocation error\n");
- free(masks);
- }
- for (size_t y = 0; y < height; y++) {
- for (size_t x = 0; x < width; x++) {
- size_t coord = x + y*width;
- uint8_t channels = 2;
- if (flood((uint8_t*)masks, labels, width, height, channels, x, y, &(((uint8_t*)masks)[coord*channels]), starting_label)) {
- starting_label++;
- }
- }
- }
- uint16_t *temp = labels;
- labels = masks;
- masks = temp;
- free(labels);
- printf("%u labels found\n", starting_label-1);
- printf("Mask dimensions: %u %u\n", width, height);
- // Get the area/ perimeter of each label
- root = NULL;
- TIME(ts_info_start);
- for (size_t y = 0; y < height; y++) {
- for (size_t x = 0; x < width; x++) {
- if (masks[x + y*width] != 0) {
- root = increase_label_area_alloc(root, masks[x + y*width]);
- if (is_on_mask_boundary(masks, width, height, x, y)) {
- increase_label_perimeter(root, masks[x + y*width]);
- }
- }
- }
- }
- TIME(ts_info_end);
- printf("Information retrieval took %f ms\n", 1000*diff_time(&ts_info_end, &ts_info_start));
-#ifdef AVL_INFO
- printf("Inorder traversal of AVL tree: ");
- print_label(root);
- printf("\n");
-#endif
- TIME(ts_filter_start);
- // Get the smallest labels
- struct AVLNode* small_label_tree = NULL;
- small_label_tree = get_small_labels(NULL, root, MIN_AREA, MIN_PERIMETER);
-
- // Remove the small labels
- for (size_t y = 0; y < height; y++) {
- for (size_t x = 0; x < width; x++) {
- if (in_uint16_t_tree(small_label_tree, masks[x + y*width])) {
- masks[x + y*width] = 0;
- }
- }
- }
- TIME(ts_filter_end);
- printf("Removing small labels took %f ms\n", 1000*diff_time(&ts_filter_end, &ts_filter_start));
- free_avl_tree(small_label_tree);
- free_avl_tree_nodes(root);
-
- // Regenerate contiguous labels
- starting_label = 1;
- labels = (uint16_t*)calloc(width*height, sizeof(uint16_t));
- if (labels == NULL) {
- fprintf(stderr, "Memory allocation error\n");
- free(masks);
- }
- for (size_t y = 0; y < height; y++) {
- for (size_t x = 0; x < width; x++) {
- size_t coord = x + y*width;
- uint8_t channels = 2;
- if (flood((uint8_t*)masks, labels, width, height, channels, x, y, &(((uint8_t*)masks)[coord*channels]), starting_label)) {
- starting_label++;
- }
- }
- }
- temp = labels;
- labels = masks;
- masks = temp;
- free(labels);
- printf("%u remaining labels found\n", starting_label-1);
- printf("Mask dimensions: %u %u\n", width, height);
- // Regenerate information after relabeling
- root = NULL;
- for (size_t y = 0; y < height; y++) {
- for (size_t x = 0; x < width; x++) {
- if (masks[x + y*width] != 0) {
- root = increase_label_area_alloc(root, masks[x + y*width]);
- if (is_on_mask_boundary(masks, width, height, x, y)) {
- increase_label_perimeter(root, masks[x + y*width]);
- }
- }
- }
- }
-#ifdef AVL_INFO
- printf("Inorder traversal of AVL tree: ");
- print_label(root);
- printf("\n");
-#endif
- free_avl_tree_nodes(root);
-
- uint16_t *new_labels;
- TIME(ts_start);
- new_labels = closeup(masks, width, height, N_DILATIONS);
- if (new_labels != NULL) {
- free(masks);
- masks = new_labels;
- }
- TIME(ts_end);
- printf("Closing up took %f ms\n", 1000*diff_time(&ts_end, &ts_start));
- //-----------------------------------------------
-
-#ifdef VISUAL
- //-----------------------------------------------
- //-RAYLIB-INIT
- //-----------------------------------------------
- SetTraceLogLevel(LOG_ERROR);
- SetConfigFlags(FLAG_WINDOW_RESIZABLE);
- const char* gui_title = "Image Manip - Useful for segmentations!";
- InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, gui_title);
-
- //-----------------------------------------------
- // (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++) {
- /// RGBA channels: Move labels to RGB
- masks[x + y*width] <<= 4;
- masks[x + y*width] |= 0x000F;
- }
- }
- //-----------------------------------------------
-
- //-----------------------------------------------
- //-RAYLIB-IMAGE-STRUCTURING----------------------
- //-----------------------------------------------
- Image RaylibImage;
- RaylibImage.width = width;
- RaylibImage.height = height;
- RaylibImage.mipmaps = 1;
- // Use Contiguous Labels
- RaylibImage.data = masks;
- RaylibImage.format = PIXELFORMAT_UNCOMPRESSED_R4G4B4A4;
- //-----------------------------------------------
-
- // Image to a Texture
- Texture2D RaylibTexture = LoadTextureFromImage(RaylibImage);
-
- // Scale the image to the viewport
- /// Source Rectangle: Original Size
- Rectangle sourceRec = { 0.0f, 0.0f, (float)width, (float)height };
- /// Destination Rectangle: Transformed Size
- Rectangle destRec = { 0.0f, 0.0f, (float)SCREEN_WIDTH, (float)(SCREEN_HEIGHT-OFFSET) };
- /// Location to begin drawing
- Vector2 origin = { (float)0, (float)-OFFSET };
-
- // Raylib boilerplate
- SetTargetFPS(60);
- Camera2D camera = { 0 };
- camera.zoom = 1.0f;
-
- // GUI Loop
- while (!WindowShouldClose()) {
- //-----------------------------------------------
- //-DRAWING---------------------------------------
- //-----------------------------------------------
- BeginDrawing();
- ClearBackground(RAYWHITE);
- BeginMode2D(camera);
- EndMode2D();
- DrawText("Image Manip", 0, 0, OFFSET, DARKGRAY);
- DrawTexturePro(RaylibTexture, sourceRec, destRec, origin, (float)0,
- RAYWHITE);
- /*
- uint32_t x = 0x49, y = 0x4A;
- uint32_t dx = 0x69 - x, dy = 0x6E - y;
- x = (SCREEN_WIDTH*x)/width;
- y = SCREEN_HEIGHT-((SCREEN_HEIGHT-OFFSET)*y)/height;
- dx = (SCREEN_WIDTH*dx)/width;
- dy = SCREEN_HEIGHT-((SCREEN_HEIGHT-OFFSET)*dy)/height;
- DrawRectangleGradientH(x, y, dx, dy, BLUE, PURPLE);
- */
- EndDrawing();
- //-----------------------------------------------
- }
-
- if (masks != NULL) {
- for (size_t y = 0; y < height; y++) {
- for (size_t x = 0; x < width; x++) {
- /// Restore labels from RGBA
- masks[x + y*width] &= 0xFFF0;
- masks[x + y*width] >>= 4;
- }
- }
- write_array("../out.bin", masks, width*height*sizeof(uint16_t));
- free(masks);
- }
- CloseWindow();
-#else
- if (masks != NULL) {
- struct bitmap_t* bitmap = uint16_to_bitmap(masks, width, height);
- char* png_name = "../out.png";
- char* bin_name = "../out.bin";
- if (bitmap != NULL) {
- if (argc > 3) {
- save_png(bitmap, argv[3]);
- } else {
- save_png(bitmap, png_name);
- }
- free(bitmap);
- }
- if (argc > 2) {
- write_array(argv[2], masks, width*height*sizeof(uint16_t));
- } else {
- write_array(bin_name, masks, width*height*sizeof(uint16_t));
- }
- free(masks);
- }
-#endif
- TIME(ts_g_end);
- printf("Finished in %f ms\n", 1000*diff_time(&ts_g_end, &ts_g_start));
- return 0;
-}