diff --git a/examples/window_mockup.png b/design/window_mockup.png similarity index 100% rename from examples/window_mockup.png rename to design/window_mockup.png diff --git a/examples/raw_importer/image_2x2_RGBA.raw b/examples/image_raw_importer/image_2x2_RGBA.raw similarity index 100% rename from examples/raw_importer/image_2x2_RGBA.raw rename to examples/image_raw_importer/image_2x2_RGBA.raw diff --git a/examples/image_raw_importer/image_raw_importer.c b/examples/image_raw_importer/image_raw_importer.c new file mode 100644 index 0000000..28af480 --- /dev/null +++ b/examples/image_raw_importer/image_raw_importer.c @@ -0,0 +1,191 @@ +/******************************************************************************************* +* +* raw_importer - image raw importer +* +* LICENSE: zlib/libpng +* +* Copyright (c) 2018 raylib technologies +* +**********************************************************************************************/ + +#include "raylib.h" + +#define RAYGUI_IMPLEMENTATION +#include "raygui.h" + +#include // Required for: strcpy() +#include // Required for: atoi() + +//---------------------------------------------------------------------------------- +// Controls Functions Declaration +//---------------------------------------------------------------------------------- +static void ImportRAW(); // Button: ImportRAW logic + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main() +{ + // Initialization + //--------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 600; + + InitWindow(screenWidth, screenHeight, "Image RAW Importer"); + + Texture2D texture = { 0 }; + + // raw_importer: controls initialization + //---------------------------------------------------------------------------------- + Vector2 windowOffset = { screenWidth/2 - 200/2, screenHeight/2 - 465/2 }; + + bool importWindowActive = false; + int widthValue = 0; + int heightValue = 0; + int pixelFormatActive = 0; + const char *pixelFormatTextList[3] = { "ONE", "TWO", "THREE" }; + int channelsActive = 0; + const char *channelsTextList[4] = { "1", "2", "3", "4" }; + int bitDepthActive = 0; + const char *bitDepthTextList[3] = { "8", "16", "32" }; + int headerSizeValue = 0; + //---------------------------------------------------------------------------------- + + // Image file info + int dataSize = 0; + char fileName[32] = "\0"; + + bool btnLoadPressed = false; + + bool imageLoaded = false; + float imageScale = 1.0f; + + SetTargetFPS(60); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + // Check if a file is dropped + if (IsFileDropped()) + { + int fileCount = 0; + char **droppedFiles = GetDroppedFiles(&fileCount); + + // Check file extensions for drag-and-drop + if ((fileCount == 1) && IsFileExtension(droppedFiles[0], ".raw")) + { + FILE *imageFile = fopen(droppedFiles[0], "rb"); + fseek(imageFile, 0L, SEEK_END); + dataSize = ftell(imageFile); + fclose(imageFile); + + // NOTE: Returned string is just a pointer to droppedFiles[0], + // we need to make a copy of that data somewhere else: fileName + strcpy(fileName, GetFileName(droppedFiles[0])); + + // Try to guess possible raw values + // Let's assume image is square, RGBA, 8 bit per channel + widthValue = round(sqrt(dataSize/4)); + heightValue = widthValue; + headerSizeValue = dataSize - widthValue*heightValue*4; + + importWindowActive = true; + } + + ClearDroppedFiles(); + } + + // Check if load button has been pressed + if (btnLoadPressed) + { + // Convert input textImageWidth, textImageHeight to int + + int format = UNCOMPRESSED_R8G8B8A8; + int channels = atoi(channelsTextList[channelsActive]); + int bpp = atoi(bitDepthTextList[bitDepthActive]); + + // Depending on channels and bit depth, select correct pixel format + if ((widthValue != 0) && (heightValue != 0) && (bpp == 8)) + { + switch (channels) + { + case 1: format = UNCOMPRESSED_GRAYSCALE; break; + case 2: format = UNCOMPRESSED_GRAY_ALPHA; break; + case 3: format = UNCOMPRESSED_R8G8B8; break; + case 4: format = UNCOMPRESSED_R8G8B8A8; break; + default: break; + } + + Image image = LoadImageRaw(fileName, widthValue, heightValue, format, headerSizeValue); + texture = LoadTextureFromImage(image); + UnloadImage(image); + + importWindowActive = false; + btnLoadPressed = false; + + if (texture.id > 0) + { + imageLoaded = true; + imageScale = (float)(screenHeight - 100)/texture.height; + } + } + } + + if (imageLoaded) imageScale += (float)GetMouseWheelMove(); // Image scale control + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(GetColor(style[DEFAULT_BACKGROUND_COLOR])); + + // raygui: controls drawing + //---------------------------------------------------------------------------------- + if (importWindowActive) + { + importWindowActive = !GuiWindowBox((Rectangle){ windowOffset.x + 0, windowOffset.y + 0, 200, 465 }, "Image RAW Import Options"); + + GuiLabel((Rectangle){ windowOffset.x + 10, windowOffset.y + 30, 65, 20 }, "Import file:"); + GuiLabel((Rectangle){ windowOffset.x + 85, windowOffset.y + 30, 75, 20 }, "Untitled.raw"); + GuiLabel((Rectangle){ windowOffset.x + 10, windowOffset.y + 50, 65, 20 }, "File size:"); + GuiLabel((Rectangle){ windowOffset.x + 85, windowOffset.y + 50, 75, 20 }, "2132421 bytes"); + GuiGroupBox((Rectangle){ windowOffset.x + 10, windowOffset.y + 85, 180, 80 }, "Resolution"); + GuiLabel((Rectangle){ windowOffset.x + 20, windowOffset.y + 100, 33, 25 }, "Width:"); + widthValue = GuiValueBox((Rectangle){ windowOffset.x + 60, windowOffset.y + 100, 80, 25 }, widthValue, 100); + GuiLabel((Rectangle){ windowOffset.x + 145, windowOffset.y + 100, 30, 25 }, "pixels"); + GuiLabel((Rectangle){ windowOffset.x + 20, windowOffset.y + 130, 33, 25 }, "Height:"); + heightValue = GuiValueBox((Rectangle){ windowOffset.x + 60, windowOffset.y + 130, 80, 25 }, heightValue, 100); + GuiLabel((Rectangle){ windowOffset.x + 145, windowOffset.y + 130, 30, 25 }, "pixels"); + GuiGroupBox((Rectangle){ windowOffset.x + 10, windowOffset.y + 180, 180, 160 }, "Pixel Format"); + pixelFormatActive = GuiComboBox((Rectangle){ windowOffset.x + 20, windowOffset.y + 195, 160, 25 }, pixelFormatTextList, 3, pixelFormatActive); + GuiLine((Rectangle){ windowOffset.x + 20, windowOffset.y + 220, 160, 20 }, 1); + GuiLabel((Rectangle){ windowOffset.x + 20, windowOffset.y + 235, 50, 20 }, "Channels:"); + channelsActive = GuiToggleGroup((Rectangle){ windowOffset.x + 20, windowOffset.y + 255, 159, 25 }, channelsTextList, 4, channelsActive); + GuiLabel((Rectangle){ windowOffset.x + 20, windowOffset.y + 285, 50, 20 }, "Bit Depth:"); + bitDepthActive = GuiToggleGroup((Rectangle){ windowOffset.x + 20, windowOffset.y + 305, 159, 25 }, bitDepthTextList, 3, bitDepthActive); + GuiGroupBox((Rectangle){ windowOffset.x + 10, windowOffset.y + 355, 180, 50 }, "Header"); + GuiLabel((Rectangle){ windowOffset.x + 25, windowOffset.y + 370, 27, 25 }, "Size:"); + headerSizeValue = GuiValueBox((Rectangle){ windowOffset.x + 55, windowOffset.y + 370, 85, 25 }, headerSizeValue, 100); + GuiLabel((Rectangle){ windowOffset.x + 145, windowOffset.y + 370, 30, 25 }, "bytes"); + + btnLoadPressed = GuiButton((Rectangle){ windowOffset.x + 10, windowOffset.y + 420, 180, 30 }, "Import RAW"); + } + + //---------------------------------------------------------------------------------- + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + if (texture.id != 0) UnloadTexture(texture); + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} diff --git a/examples/raw_importer/raw_importer.c b/examples/image_raw_importer/image_raw_importer.old.c similarity index 94% rename from examples/raw_importer/raw_importer.c rename to examples/image_raw_importer/image_raw_importer.old.c index 27d4960..55ca268 100644 --- a/examples/raw_importer/raw_importer.c +++ b/examples/image_raw_importer/image_raw_importer.old.c @@ -36,10 +36,10 @@ int main(int argc, char *argv[0]) bool btnLoadPressed = false; - int buttonToggleChannel = 3; + int buttonToggleChannel = 1; int buttonToggleDepth = 0; - char *arrayChannel[4] = { "1", "2", "3", "4" }; - char *arrayDepth[3] = { "8", "16", "32" }; + const char *arrayChannel[4] = { "1", "2", "3", "4" }; + const char *arrayDepth[3] = { "8", "16", "32" }; // Image file info int dataSize = 0; @@ -149,24 +149,24 @@ int main(int argc, char *argv[0]) //---------------------------------------------------------------------------------- BeginDrawing(); - ClearBackground(GuiGetBackgroundColor()); + ClearBackground(RAYWHITE); - DrawRectangleLines(10, 10, SCREEN_WIDTH - 20, SCREEN_HEIGHT - 20, GuiLinesColor()); + DrawRectangleLines(10, 10, SCREEN_WIDTH - 20, SCREEN_HEIGHT - 20, BLUE); if (texture.id != 0) { DrawTextureEx(texture, (Vector2){ SCREEN_WIDTH/2 - texture.width*imageScale/2, SCREEN_HEIGHT/2 - texture.height*imageScale/2 }, 0, imageScale, WHITE); - DrawText(FormatText("SCALE x%.0f", imageScale), 20, SCREEN_HEIGHT - 40, 20, GuiTextColor()); + DrawText(FormatText("SCALE x%.0f", imageScale), 20, SCREEN_HEIGHT - 40, 20, BLUE); } - else DrawText("drag & drop RAW image file", 320, 180, 10, GuiTextColor()); + else DrawText("drag & drop RAW image file", 320, 180, 10, BLUE); if (showImportPanel) { DrawRectangle(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, Fade(LIGHTGRAY, 0.5f)); - DrawRectangleRec(panel, GuiBackgroundColor()); - DrawRectangleLines(panel.x, panel.y, panel.width, panel.height, GuiLinesColor()); + DrawRectangleRec(panel, RAYWHITE); + DrawRectangleLines(panel.x, panel.y, panel.width, panel.height, BLUE); GuiLabel((Rectangle){ panel.x + 10, panel.y + 10, 0, 0 }, FormatText("Import file: %s", fileName)); GuiLabel((Rectangle){ panel.x + 10, panel.y + 30, 0, 0 }, FormatText("File size: %i bytes", dataSize)); @@ -177,12 +177,12 @@ int main(int argc, char *argv[0]) GuiLabel((Rectangle){ panel.x + 20, panel.y + 80, 0, 0 }, "Width:"); GuiLabel((Rectangle){ panel.x + 150, panel.y + 80, 0, 0 }, "pixels"); - GuiTextBox((Rectangle){ panel.x + 60, panel.y + 75, 80, 20 }, textImageWidth, 4); + GuiTextBox((Rectangle){ panel.x + 60, panel.y + 75, 80, 20 }, textImageWidth, 4, false); GuiLabel((Rectangle){ panel.x + 20, panel.y + 105, 0, 0 }, "Height:"); GuiLabel((Rectangle){ panel.x + 150, panel.y + 105, 0, 0 }, "pixels"); - GuiTextBox((Rectangle){ panel.x + 60, panel.y + 100, 80, 20 }, textImageHeight, 4); + GuiTextBox((Rectangle){ panel.x + 60, panel.y + 100, 80, 20 }, textImageHeight, 4, false); // ----- Pixel data panel ----- GuiGroupBox((Rectangle){ panel.x + 10, panel.y + 155, 180, 110 }, "Pixels Data"); @@ -199,7 +199,7 @@ int main(int argc, char *argv[0]) GuiLabel((Rectangle){ panel.x + 20, panel.y + 305, 0, 0 }, "Size:"); GuiLabel((Rectangle){ panel.x + 150, panel.y + 305, 0, 0 }, "bytes"); - GuiTextBox((Rectangle){ panel.x + 60, panel.y + 300, 80, 20 }, textImageSize, 4); + GuiTextBox((Rectangle){ panel.x + 60, panel.y + 300, 80, 20 }, textImageSize, 4, false); btnLoadPressed = GuiButton((Rectangle){ panel.x + 10, panel.y + 350, 180, 24 }, "Load RAW Image"); } diff --git a/examples/raw_importer/raw_importer_REF.png b/examples/image_raw_importer/raw_importer_REF.png similarity index 100% rename from examples/raw_importer/raw_importer_REF.png rename to examples/image_raw_importer/raw_importer_REF.png diff --git a/examples/raw_importer/raw_importer_REV0.png b/examples/image_raw_importer/raw_importer_REV0.png similarity index 100% rename from examples/raw_importer/raw_importer_REV0.png rename to examples/image_raw_importer/raw_importer_REV0.png diff --git a/examples/raw_importer/raw_importer_REV1.png b/examples/image_raw_importer/raw_importer_REV1.png similarity index 100% rename from examples/raw_importer/raw_importer_REV1.png rename to examples/image_raw_importer/raw_importer_REV1.png diff --git a/examples/raw_importer/raw_importer_REV2.png b/examples/image_raw_importer/raw_importer_REV2.png similarity index 100% rename from examples/raw_importer/raw_importer_REV2.png rename to examples/image_raw_importer/raw_importer_REV2.png diff --git a/examples/raw_importer/raw_importer_REV3.png b/examples/image_raw_importer/raw_importer_REV3.png similarity index 100% rename from examples/raw_importer/raw_importer_REV3.png rename to examples/image_raw_importer/raw_importer_REV3.png diff --git a/examples/raw_importer/raw_importer_REV4.png b/examples/image_raw_importer/raw_importer_REV4.png similarity index 100% rename from examples/raw_importer/raw_importer_REV4.png rename to examples/image_raw_importer/raw_importer_REV4.png diff --git a/examples/image_raw_importer/raw_importer_REV5.png b/examples/image_raw_importer/raw_importer_REV5.png new file mode 100644 index 0000000..3f5e012 Binary files /dev/null and b/examples/image_raw_importer/raw_importer_REV5.png differ