load image in gui
This commit is contained in:
		
							parent
							
								
									1cb30b1ca7
								
							
						
					
					
						commit
						d7a54361f5
					
				
					 2 changed files with 79 additions and 185 deletions
				
			
		
							
								
								
									
										2
									
								
								justfile
									
										
									
									
									
								
							
							
						
						
									
										2
									
								
								justfile
									
										
									
									
									
								
							| 
						 | 
					@ -20,7 +20,7 @@ run-cli: build-cli
 | 
				
			||||||
	{{exe_cli}} lenna.png 0 0 0 0
 | 
						{{exe_cli}} lenna.png 0 0 0 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
run-gui: build-gui
 | 
					run-gui: build-gui
 | 
				
			||||||
	nixGL {{exe_gui}}
 | 
						nixGL {{exe_gui}} lenna.png
 | 
				
			||||||
 | 
					
 | 
				
			||||||
generate-build:
 | 
					generate-build:
 | 
				
			||||||
	git clean -xf src/
 | 
						git clean -xf src/
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										254
									
								
								src/main_gui.cpp
									
										
									
									
									
								
							
							
						
						
									
										254
									
								
								src/main_gui.cpp
									
										
									
									
									
								
							| 
						 | 
					@ -1,198 +1,92 @@
 | 
				
			||||||
/*******************************************************************************************
 | 
					#include <cstdlib>
 | 
				
			||||||
 *
 | 
					#include <filesystem>
 | 
				
			||||||
 *   raylib [textures] example - Image processing
 | 
					#include <iostream>
 | 
				
			||||||
 *
 | 
					#include <vector>
 | 
				
			||||||
 *   NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU
 | 
					 | 
				
			||||||
 *memory (VRAM)
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 *   This example has been created using raylib 3.5 (www.raylib.com)
 | 
					 | 
				
			||||||
 *   raylib is licensed under an unmodified zlib/libpng license (View raylib.h
 | 
					 | 
				
			||||||
 *for details)
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 *   Copyright (c) 2016 Ramon Santamaria (@raysan5)
 | 
					 | 
				
			||||||
 *
 | 
					 | 
				
			||||||
 ********************************************************************************************/
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "raylib.h"
 | 
					namespace raylib {
 | 
				
			||||||
 | 
					#include <raylib.h>
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <stdlib.h>  // Required for: free()
 | 
					#include "bounding_box.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define NUM_PROCESSES 8
 | 
					namespace fs = std::filesystem;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef enum {
 | 
					struct Button {
 | 
				
			||||||
    NONE = 0,
 | 
					    raylib::Rectangle rect;
 | 
				
			||||||
    COLOR_GRAYSCALE,
 | 
					    std::string text;
 | 
				
			||||||
    COLOR_TINT,
 | 
					    bool hover = false;
 | 
				
			||||||
    COLOR_INVERT,
 | 
					 | 
				
			||||||
    COLOR_CONTRAST,
 | 
					 | 
				
			||||||
    COLOR_BRIGHTNESS,
 | 
					 | 
				
			||||||
    FLIP_VERTICAL,
 | 
					 | 
				
			||||||
    FLIP_HORIZONTAL
 | 
					 | 
				
			||||||
} ImageProcess;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
static const char* processText[] = {
 | 
					    bool pressed() {
 | 
				
			||||||
    "NO PROCESSING",  "COLOR GRAYSCALE",  "COLOR TINT",    "COLOR INVERT",
 | 
					        hover =
 | 
				
			||||||
    "COLOR CONTRAST", "COLOR BRIGHTNESS", "FLIP VERTICAL", "FLIP HORIZONTAL"};
 | 
					            raylib::CheckCollisionPointRec(raylib::GetMousePosition(), rect);
 | 
				
			||||||
 | 
					        return hover and
 | 
				
			||||||
 | 
					               raylib::IsMouseButtonReleased(raylib::MOUSE_BUTTON_LEFT);
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int main(void) {
 | 
					    void draw() const {
 | 
				
			||||||
    // Initialization
 | 
					        raylib::DrawRectangleRec(rect, raylib::LIGHTGRAY);
 | 
				
			||||||
    //--------------------------------------------------------------------------------------
 | 
					        raylib::DrawRectangleLines(rect.x, rect.y, rect.width, rect.height,
 | 
				
			||||||
    const int screenWidth = 800;
 | 
					                                   raylib::BLUE);
 | 
				
			||||||
    const int screenHeight = 450;
 | 
					        raylib::DrawText(text.c_str(),
 | 
				
			||||||
 | 
					                         (rect.x + rect.width / 2 -
 | 
				
			||||||
 | 
					                          raylib::MeasureText(text.c_str(), 10) / 2),
 | 
				
			||||||
 | 
					                         rect.y + 11, 10, raylib::DARKBLUE);
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    InitWindow(screenWidth, screenHeight,
 | 
					void draw(const BoundingBox& box) {}
 | 
				
			||||||
               "raylib [textures] example - image processing");
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // NOTE: Textures MUST be loaded after Window initialization (OpenGL context
 | 
					int main(int argc, const char* argv[]) {
 | 
				
			||||||
    // is required)
 | 
					    if (argc < 2) {
 | 
				
			||||||
 | 
					        std::cerr << "Usage: " << argv[0] << " path/to/image\n";
 | 
				
			||||||
    Image imOrigin =
 | 
					        return 1;
 | 
				
			||||||
        LoadImage("resources/parrots.png");  // Loaded in CPU memory (RAM)
 | 
					 | 
				
			||||||
    ImageFormat(
 | 
					 | 
				
			||||||
        &imOrigin,
 | 
					 | 
				
			||||||
        PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);  // Format image to RGBA 32bit
 | 
					 | 
				
			||||||
                                             // (required for texture update)
 | 
					 | 
				
			||||||
                                             // <-- ISSUE
 | 
					 | 
				
			||||||
    Texture2D texture = LoadTextureFromImage(
 | 
					 | 
				
			||||||
        imOrigin);  // Image converted to texture, GPU memory (VRAM)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    Image imCopy = ImageCopy(imOrigin);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    int currentProcess = NONE;
 | 
					 | 
				
			||||||
    bool textureReload = false;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    Rectangle toggleRecs[NUM_PROCESSES] = {{0}};
 | 
					 | 
				
			||||||
    int mouseHoverRec = -1;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    for (int i = 0; i < NUM_PROCESSES; i++)
 | 
					 | 
				
			||||||
        toggleRecs[i] = (Rectangle){40.0f, (float)(50 + 32 * i), 150.0f, 30.0f};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    SetTargetFPS(60);
 | 
					 | 
				
			||||||
    //---------------------------------------------------------------------------------------
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // Main game loop
 | 
					 | 
				
			||||||
    while (!WindowShouldClose())  // Detect window close button or ESC key
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        // Update
 | 
					 | 
				
			||||||
        //----------------------------------------------------------------------------------
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // Mouse toggle group logic
 | 
					 | 
				
			||||||
        for (int i = 0; i < NUM_PROCESSES; i++) {
 | 
					 | 
				
			||||||
            if (CheckCollisionPointRec(GetMousePosition(), toggleRecs[i])) {
 | 
					 | 
				
			||||||
                mouseHoverRec = i;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) {
 | 
					 | 
				
			||||||
                    currentProcess = i;
 | 
					 | 
				
			||||||
                    textureReload = true;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                break;
 | 
					 | 
				
			||||||
            } else
 | 
					 | 
				
			||||||
                mouseHoverRec = -1;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Keyboard toggle group logic
 | 
					    const fs::path input(argv[1]);
 | 
				
			||||||
        if (IsKeyPressed(KEY_DOWN)) {
 | 
					    if (!fs::exists(input)) {
 | 
				
			||||||
            currentProcess++;
 | 
					        std::cerr << "Input file " << input << " does not exist.\n";
 | 
				
			||||||
            if (currentProcess > (NUM_PROCESSES - 1))
 | 
					        return 1;
 | 
				
			||||||
                currentProcess = 0;
 | 
					    }
 | 
				
			||||||
            textureReload = true;
 | 
					    raylib::Vector2 win_size = {800, 450};
 | 
				
			||||||
        } else if (IsKeyPressed(KEY_UP)) {
 | 
					    const int padding = 5;
 | 
				
			||||||
            currentProcess--;
 | 
					    raylib::Vector2 in_offset = {padding, 40};
 | 
				
			||||||
            if (currentProcess < 0)
 | 
					
 | 
				
			||||||
                currentProcess = 7;
 | 
					    raylib::InitWindow(win_size.x, win_size.y, "netatmo - rect packing");
 | 
				
			||||||
            textureReload = true;
 | 
					    raylib::SetTargetFPS(60);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    raylib::Image imOrigin = raylib::LoadImage(input.c_str());
 | 
				
			||||||
 | 
					    raylib::ImageFormat(&imOrigin, raylib::PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);
 | 
				
			||||||
 | 
					    raylib::Texture2D texture = raylib::LoadTextureFromImage(imOrigin);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    win_size.x = std::max(win_size.x, in_offset.x + texture.width + padding);
 | 
				
			||||||
 | 
					    win_size.y = std::max(win_size.y, in_offset.y + texture.height + padding);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    raylib::SetWindowSize(win_size.x, win_size.y);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Button b_add_box = {{padding, padding, 150, 30}, "Add box"};
 | 
				
			||||||
 | 
					    std::vector<BoundingBox> bboxes;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    while (!raylib::WindowShouldClose()) {
 | 
				
			||||||
 | 
					        if (b_add_box.pressed()) {
 | 
				
			||||||
 | 
					            bboxes.push_back(BoundingBox({0, 0, 20, 20}));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Reload texture when required
 | 
					        raylib::BeginDrawing();
 | 
				
			||||||
        if (textureReload) {
 | 
					        raylib::ClearBackground(raylib::RAYWHITE);
 | 
				
			||||||
            UnloadImage(imCopy);  // Unload image-copy data
 | 
					 | 
				
			||||||
            imCopy =
 | 
					 | 
				
			||||||
                ImageCopy(imOrigin);  // Restore image-copy from image-origin
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // NOTE: Image processing is a costly CPU process to be done every
 | 
					        b_add_box.draw();
 | 
				
			||||||
            // frame, If image processing is required in a frame-basis, it
 | 
					
 | 
				
			||||||
            // should be done with a texture and by shaders
 | 
					        raylib::DrawTexture(texture, in_offset.x, in_offset.y, raylib::WHITE);
 | 
				
			||||||
            switch (currentProcess) {
 | 
					        raylib::DrawRectangleLines(in_offset.x, in_offset.y, texture.width,
 | 
				
			||||||
                case COLOR_GRAYSCALE:
 | 
					                                   texture.height, raylib::BLACK);
 | 
				
			||||||
                    ImageColorGrayscale(&imCopy);
 | 
					
 | 
				
			||||||
                    break;
 | 
					        raylib::EndDrawing();
 | 
				
			||||||
                case COLOR_TINT:
 | 
					 | 
				
			||||||
                    ImageColorTint(&imCopy, GREEN);
 | 
					 | 
				
			||||||
                    break;
 | 
					 | 
				
			||||||
                case COLOR_INVERT:
 | 
					 | 
				
			||||||
                    ImageColorInvert(&imCopy);
 | 
					 | 
				
			||||||
                    break;
 | 
					 | 
				
			||||||
                case COLOR_CONTRAST:
 | 
					 | 
				
			||||||
                    ImageColorContrast(&imCopy, -40);
 | 
					 | 
				
			||||||
                    break;
 | 
					 | 
				
			||||||
                case COLOR_BRIGHTNESS:
 | 
					 | 
				
			||||||
                    ImageColorBrightness(&imCopy, -80);
 | 
					 | 
				
			||||||
                    break;
 | 
					 | 
				
			||||||
                case FLIP_VERTICAL:
 | 
					 | 
				
			||||||
                    ImageFlipVertical(&imCopy);
 | 
					 | 
				
			||||||
                    break;
 | 
					 | 
				
			||||||
                case FLIP_HORIZONTAL:
 | 
					 | 
				
			||||||
                    ImageFlipHorizontal(&imCopy);
 | 
					 | 
				
			||||||
                    break;
 | 
					 | 
				
			||||||
                default:
 | 
					 | 
				
			||||||
                    break;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            Color* pixels = LoadImageColors(
 | 
					    // Cleanup
 | 
				
			||||||
                imCopy);  // Load pixel data from image (RGBA 32bit)
 | 
					    raylib::UnloadTexture(texture);
 | 
				
			||||||
            UpdateTexture(texture,
 | 
					    raylib::UnloadImage(imOrigin);
 | 
				
			||||||
                          pixels);      // Update texture with new image data
 | 
					    raylib::CloseWindow();
 | 
				
			||||||
            UnloadImageColors(pixels);  // Unload pixels data from RAM
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            textureReload = false;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        //----------------------------------------------------------------------------------
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // Draw
 | 
					 | 
				
			||||||
        //----------------------------------------------------------------------------------
 | 
					 | 
				
			||||||
        BeginDrawing();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        ClearBackground(RAYWHITE);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        DrawText("IMAGE PROCESSING:", 40, 30, 10, DARKGRAY);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // Draw rectangles
 | 
					 | 
				
			||||||
        for (int i = 0; i < NUM_PROCESSES; i++) {
 | 
					 | 
				
			||||||
            DrawRectangleRec(toggleRecs[i],
 | 
					 | 
				
			||||||
                             ((i == currentProcess) || (i == mouseHoverRec))
 | 
					 | 
				
			||||||
                                 ? SKYBLUE
 | 
					 | 
				
			||||||
                                 : LIGHTGRAY);
 | 
					 | 
				
			||||||
            DrawRectangleLines(
 | 
					 | 
				
			||||||
                (int)toggleRecs[i].x, (int)toggleRecs[i].y,
 | 
					 | 
				
			||||||
                (int)toggleRecs[i].width, (int)toggleRecs[i].height,
 | 
					 | 
				
			||||||
                ((i == currentProcess) || (i == mouseHoverRec)) ? BLUE : GRAY);
 | 
					 | 
				
			||||||
            DrawText(processText[i],
 | 
					 | 
				
			||||||
                     (int)(toggleRecs[i].x + toggleRecs[i].width / 2 -
 | 
					 | 
				
			||||||
                           MeasureText(processText[i], 10) / 2),
 | 
					 | 
				
			||||||
                     (int)toggleRecs[i].y + 11, 10,
 | 
					 | 
				
			||||||
                     ((i == currentProcess) || (i == mouseHoverRec))
 | 
					 | 
				
			||||||
                         ? DARKBLUE
 | 
					 | 
				
			||||||
                         : DARKGRAY);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        DrawTexture(texture, screenWidth - texture.width - 60,
 | 
					 | 
				
			||||||
                    screenHeight / 2 - texture.height / 2, WHITE);
 | 
					 | 
				
			||||||
        DrawRectangleLines(screenWidth - texture.width - 60,
 | 
					 | 
				
			||||||
                           screenHeight / 2 - texture.height / 2, texture.width,
 | 
					 | 
				
			||||||
                           texture.height, BLACK);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        EndDrawing();
 | 
					 | 
				
			||||||
        //----------------------------------------------------------------------------------
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // De-Initialization
 | 
					 | 
				
			||||||
    //--------------------------------------------------------------------------------------
 | 
					 | 
				
			||||||
    UnloadTexture(texture);  // Unload texture from VRAM
 | 
					 | 
				
			||||||
    UnloadImage(imOrigin);   // Unload image-origin from RAM
 | 
					 | 
				
			||||||
    UnloadImage(imCopy);     // Unload image-copy from RAM
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    CloseWindow();  // Close window and OpenGL context
 | 
					 | 
				
			||||||
    //--------------------------------------------------------------------------------------
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue