Renamed folder and reviewed sample

Working on generated raw importer
This commit is contained in:
Ray San
2018-05-23 13:24:21 +02:00
parent 614f99a7cb
commit 13e5dae316
11 changed files with 203 additions and 12 deletions

View File

Before

Width:  |  Height:  |  Size: 6.2 KiB

After

Width:  |  Height:  |  Size: 6.2 KiB

View File

@ -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 <string.h> // Required for: strcpy()
#include <stdlib.h> // 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;
}

View File

@ -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");
}

View File

Before

Width:  |  Height:  |  Size: 9.3 KiB

After

Width:  |  Height:  |  Size: 9.3 KiB

View File

Before

Width:  |  Height:  |  Size: 3.7 KiB

After

Width:  |  Height:  |  Size: 3.7 KiB

View File

Before

Width:  |  Height:  |  Size: 3.9 KiB

After

Width:  |  Height:  |  Size: 3.9 KiB

View File

Before

Width:  |  Height:  |  Size: 3.0 KiB

After

Width:  |  Height:  |  Size: 3.0 KiB

View File

Before

Width:  |  Height:  |  Size: 3.7 KiB

After

Width:  |  Height:  |  Size: 3.7 KiB

View File

Before

Width:  |  Height:  |  Size: 4.4 KiB

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 KiB