From 45171a3870915571c2bae4466a59eb2ebdf42b93 Mon Sep 17 00:00:00 2001 From: Christian C Date: Thu, 6 Mar 2025 16:05:50 -0800 Subject: Modularize Visual UX --- Makefile | 10 ++++- include/snippets/raylib_block.c | 93 ++++++++++++++++++++++++++++++++++++++++ src/main.c | 94 +---------------------------------------- 3 files changed, 103 insertions(+), 94 deletions(-) create mode 100644 include/snippets/raylib_block.c diff --git a/Makefile b/Makefile index 7da1156..26be076 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ PKGS=libtiff-4 libpng -#PKGS+=raylib +DEFINES= EXE=prog BUILD_DIR=build/ OBJ_DIR=$(BUILD_DIR)obj/ @@ -11,6 +11,13 @@ OBJS=$(OBJS_sub:.c=.o) OBJ_DIRS_sub=$(shell find $(SRC_DIR) -type d) OBJ_DIRS=$(subst $(SRC_DIR),$(OBJ_DIR),$(OBJ_DIRS_sub)) +# Include raylib if we want a visual experience +VISUAL?= +ifdef VISUAL +PKGS+=raylib +DEFINES+=-DVISUAL +endif + ifeq ($(shell uname -s),Linux) PKGCONF=pkgconf endif @@ -21,6 +28,7 @@ endif CFLAGS= CFLAGS+=$(shell $(PKGCONF) --cflags $(PKGS)) CFLAGS+=-I$(INC_DIR) +CFLAGS+=$(DEFINES) LDFLAGS= LDFLAGS+=$(shell $(PKGCONF) --libs $(PKGS)) diff --git a/include/snippets/raylib_block.c b/include/snippets/raylib_block.c new file mode 100644 index 0000000..ef81fd2 --- /dev/null +++ b/include/snippets/raylib_block.c @@ -0,0 +1,93 @@ + //----------------------------------------------- + //-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(); + //----------------------------------------------- + } + + //----------------------------------------------- + //-SAVE-MASK-AS-BINARY-AND-PNG------------------- + //----------------------------------------------- + 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; + } + } + struct bitmap_t* bitmap = uint16_to_bitmap(masks, width, height); + if (bitmap != NULL) { + save_png(bitmap, png_file); + free(bitmap); + } + write_array(bin_file, masks, width*height*sizeof(uint16_t)); + free(masks); + } + CloseWindow(); diff --git a/src/main.c b/src/main.c index fdabaf5..3643ea9 100644 --- a/src/main.c +++ b/src/main.c @@ -188,99 +188,7 @@ int main(int argc, char** argv) //----------------------------------------------- #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(); - //----------------------------------------------- - } - - //----------------------------------------------- - //-SAVE-MASK-AS-BINARY-AND-PNG------------------- - //----------------------------------------------- - 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; - } - } - struct bitmap_t* bitmap = uint16_to_bitmap(masks, width, height); - if (bitmap != NULL) { - save_png(bitmap, png_file); - free(bitmap); - } - write_array(bin_file, masks, width*height*sizeof(uint16_t)); - free(masks); - } - CloseWindow(); +#include #else //----------------------------------------------- //-SAVE-MASK-AS-BINARY-AND-PNG------------------- -- cgit v1.2.1