From c269b712134eb5f9db7eacac44d240434018526e Mon Sep 17 00:00:00 2001 From: Christian C Date: Tue, 4 Mar 2025 16:24:22 -0800 Subject: Extended Commenting --- src/main.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 16 deletions(-) diff --git a/src/main.c b/src/main.c index 95b3e6d..246fae0 100644 --- a/src/main.c +++ b/src/main.c @@ -17,11 +17,17 @@ #define FALSE 0 #define TRUE 1 +//----------------------------------------------- +// Difference in Time +// Compute the difference between timespec structs double diff_timespec(struct timespec *time1, struct timespec *time0) { return (time1->tv_sec - time0->tv_sec) + (time1->tv_nsec - time0->tv_nsec) / 1000000000.0; } +//----------------------------------------------- +// Color Equal to Background +// Background: zeros in RGB, alpha can be whatever bool_t color_zero(uint8_t* color1, size_t channels) { for (size_t idx = 0; idx < channels; idx++) { if (idx == 3) { @@ -34,6 +40,9 @@ bool_t color_zero(uint8_t* color1, size_t channels) { return TRUE; } +//----------------------------------------------- +// Color Equality +// Determine if two colors are identical bool_t color_equal(uint8_t* color1, uint8_t* color2, size_t channels) { for (size_t idx = 0; idx < channels; idx++) { if (color1[idx] != color2[idx]) { @@ -43,6 +52,8 @@ bool_t color_equal(uint8_t* color1, uint8_t* color2, size_t channels) { return TRUE; } +//----------------------------------------------- +// Print out the `channels` length color vector void print_color(uint8_t* color, size_t channels) { for (size_t index = 0; index < channels; index++) { printf("%hhu ", color[index]); @@ -50,7 +61,16 @@ void print_color(uint8_t* color, size_t channels) { printf("\n"); } -bool_t flood(uint8_t* image, uint16_t* mask, size_t width, size_t height, size_t channels, size_t x, size_t y, uint8_t* color_old, uint16_t label) +//----------------------------------------------- +// Flood +// Floods a mask from a given set of image to determine the contiguous regions +// 1. Check that the (x,y) is within the picture +// 2. Check if the (x,y) coordinate in the mask is unused +// 3. Check if the (x,y) coordinate in the image is non-background +// 4. Check if the (x,y) coordinate in the image is the same color as the fill color +// 5. If all hold, set the label for the pixel, and check each neighbor +// Otherwise, stop flooding +bool_t flood(uint8_t* image, uint16_t* mask, size_t width, size_t height, size_t channels, size_t x, size_t y, uint8_t* fill_color, uint16_t label) { if ((x >= width) | (y >= height)) { return FALSE; @@ -62,12 +82,12 @@ bool_t flood(uint8_t* image, uint16_t* mask, size_t width, size_t height, size_t if (color_zero(&(image[coord*channels]), channels)) { return FALSE; } - if (color_equal(&(image[coord*channels]), color_old, channels)) { + if (color_equal(&(image[coord*channels]), fill_color, channels)) { mask[coord] = label; - flood(image, mask, width, height, channels, x, y+1, color_old, label); - flood(image, mask, width, height, channels, x, y-1, color_old, label); - flood(image, mask, width, height, channels, x+1, y, color_old, label); - flood(image, mask, width, height, channels, x-1, y, color_old, label); + flood(image, mask, width, height, channels, x, y+1, fill_color, label); + flood(image, mask, width, height, channels, x, y-1, fill_color, label); + flood(image, mask, width, height, channels, x+1, y, fill_color, label); + flood(image, mask, width, height, channels, x-1, y, fill_color, label); return TRUE; } return FALSE; @@ -141,7 +161,6 @@ int main() //-FLOOD-FILL-SEGMENTATION----------------------- //-CONTIGUOUS-REGION-FINDING--------------------- //----------------------------------------------- - // Flood fill uint16_t starting_label = 1; uint16_t *labels = NULL; labels = (uint16_t*)calloc(width*height,sizeof(uint16_t)); @@ -151,6 +170,8 @@ int main() 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; @@ -171,7 +192,12 @@ int main() printf("Time difference: %.3fms\n", 1000*diff_timespec(&ts_end, &ts_start)); printf("N_labels: %u\n", starting_label-1); - uint32_t *raster = (uint32_t*)_TIFFmalloc(width*height*sizeof(uint32_t)); + 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); @@ -188,10 +214,20 @@ int main() TIFFClose(tif); return 1; } + */ + //----------------------------------------------- + //----------------------------------------------- + //-RAYLIB-IMAGE-STRUCTURING---------------------- + //----------------------------------------------- Image RaylibImage; - //RaylibImage.data = raster; - //RaylibImage.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8; + 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; @@ -202,23 +238,28 @@ int main() } else { assert(0); } + // Use Contiguous Labels RaylibImage.data = labels; RaylibImage.format = PIXELFORMAT_UNCOMPRESSED_R4G4B4A4; + //----------------------------------------------- - RaylibImage.width = width; - RaylibImage.height = height; - RaylibImage.mipmaps = 1; + // 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--------------------------------------- @@ -230,6 +271,7 @@ int main() 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; @@ -237,6 +279,7 @@ int main() dx = (SCREEN_WIDTH*dx)/width; dy = SCREEN_HEIGHT-((SCREEN_HEIGHT-OFFSET)*dy)/height; DrawRectangleGradientH(x, y, dx, dy, BLUE, PURPLE); + */ EndDrawing(); //----------------------------------------------- } @@ -244,8 +287,12 @@ int main() if (labels != NULL) { free(labels); } - free(raster_8); - _TIFFfree(raster); + if (raster_8 != NULL) { + free(raster_8); + } + if (raster != NULL) { + _TIFFfree(raster); + } TIFFClose(tif); CloseWindow(); -- cgit v1.2.1