2018-02-14 14:00:44 +01:00
|
|
|
/*******************************************************************************************
|
|
|
|
*
|
2018-11-08 10:41:31 +01:00
|
|
|
* raygui - image exporter
|
2018-02-14 14:00:44 +01:00
|
|
|
*
|
2018-11-08 10:41:31 +01:00
|
|
|
* DEPENDENCIES:
|
2021-10-05 14:10:35 +02:00
|
|
|
* raylib 4.0 - Windowing/input management and drawing.
|
|
|
|
* raygui 3.0 - Immediate-mode GUI controls.
|
2018-11-08 10:41:31 +01:00
|
|
|
*
|
|
|
|
* COMPILATION (Windows - MinGW):
|
|
|
|
* gcc -o $(NAME_PART).exe $(FILE_NAME) -I../../src -lraylib -lopengl32 -lgdi32 -std=c99
|
2018-02-14 14:00:44 +01:00
|
|
|
*
|
2019-07-23 20:09:07 -03:00
|
|
|
* LICENSE: zlib/libpng
|
|
|
|
*
|
2021-12-31 20:06:32 +01:00
|
|
|
* Copyright (c) 2015-2022 Ramon Santamaria (@raysan5)
|
2018-02-14 14:00:44 +01:00
|
|
|
*
|
|
|
|
********************************************************************************************/
|
|
|
|
|
|
|
|
#include "raylib.h"
|
|
|
|
|
|
|
|
#define RAYGUI_IMPLEMENTATION
|
2019-07-05 20:47:15 +02:00
|
|
|
#include "../../src/raygui.h"
|
2018-02-14 14:00:44 +01:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------
|
|
|
|
// Program main entry point
|
|
|
|
//------------------------------------------------------------------------------------
|
2022-06-01 20:40:42 +02:00
|
|
|
int main(int argc, char *argv[])
|
2018-02-14 14:00:44 +01:00
|
|
|
{
|
|
|
|
// Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
2018-05-30 13:54:28 +02:00
|
|
|
const int screenWidth = 800;
|
|
|
|
const int screenHeight = 450;
|
2018-02-14 14:00:44 +01:00
|
|
|
|
2018-11-08 10:41:31 +01:00
|
|
|
InitWindow(screenWidth, screenHeight, "raygui - image exporter");
|
2018-02-14 14:00:44 +01:00
|
|
|
|
2018-11-08 10:41:31 +01:00
|
|
|
// GUI controls initialization
|
|
|
|
//----------------------------------------------------------------------------------
|
2018-05-30 23:49:59 +02:00
|
|
|
Rectangle windowBoxRec = { screenWidth/2 - 110, screenHeight/2 - 100, 220, 190 };
|
2018-05-30 13:54:28 +02:00
|
|
|
bool windowBoxActive = false;
|
2018-11-08 10:41:31 +01:00
|
|
|
|
2018-05-29 14:00:16 +02:00
|
|
|
int fileFormatActive = 0;
|
2018-05-30 13:54:28 +02:00
|
|
|
const char *fileFormatTextList[3] = { "IMAGE (.png)", "DATA (.raw)", "CODE (.h)" };
|
2018-04-06 12:24:10 +02:00
|
|
|
|
2018-11-08 10:41:31 +01:00
|
|
|
int pixelFormatActive = 0;
|
|
|
|
const char *pixelFormatTextList[7] = { "GRAYSCALE", "GRAY ALPHA", "R5G6B5", "R8G8B8", "R5G5B5A1", "R4G4B4A4", "R8G8B8A8" };
|
|
|
|
|
2019-08-16 16:07:07 +02:00
|
|
|
bool textBoxEditMode = false;
|
2018-11-08 10:41:31 +01:00
|
|
|
char fileName[32] = "untitled";
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
2018-05-30 13:54:28 +02:00
|
|
|
Image image = { 0 };
|
|
|
|
Texture2D texture = { 0 };
|
2018-05-30 23:49:59 +02:00
|
|
|
|
2018-05-30 13:54:28 +02:00
|
|
|
bool imageLoaded = false;
|
|
|
|
float imageScale = 1.0f;
|
2020-10-05 20:08:47 +02:00
|
|
|
Rectangle imageRec = { 0 };
|
2018-05-30 23:49:59 +02:00
|
|
|
|
|
|
|
bool btnExport = false;
|
2018-04-06 12:24:10 +02:00
|
|
|
|
2018-02-14 14:00:44 +01:00
|
|
|
SetTargetFPS(60);
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Main game loop
|
|
|
|
while (!WindowShouldClose()) // Detect window close button or ESC key
|
|
|
|
{
|
|
|
|
// Update
|
|
|
|
//----------------------------------------------------------------------------------
|
2018-05-29 14:00:16 +02:00
|
|
|
if (IsFileDropped())
|
|
|
|
{
|
|
|
|
int fileCount = 0;
|
2022-06-06 20:40:09 +02:00
|
|
|
char **droppedFiles = LoadDroppedFiles(&fileCount);
|
2018-05-29 14:00:16 +02:00
|
|
|
|
2018-05-30 13:54:28 +02:00
|
|
|
if (fileCount == 1)
|
2018-05-29 14:00:16 +02:00
|
|
|
{
|
2018-05-30 13:54:28 +02:00
|
|
|
Image imTemp = LoadImage(droppedFiles[0]);
|
2018-05-29 14:00:16 +02:00
|
|
|
|
2018-05-30 13:54:28 +02:00
|
|
|
if (imTemp.data != NULL)
|
|
|
|
{
|
|
|
|
UnloadImage(image);
|
|
|
|
image = imTemp;
|
|
|
|
|
|
|
|
UnloadTexture(texture);
|
|
|
|
texture = LoadTextureFromImage(image);
|
|
|
|
|
|
|
|
imageLoaded = true;
|
|
|
|
pixelFormatActive = image.format - 1;
|
|
|
|
|
|
|
|
if (texture.height > texture.width) imageScale = (float)(screenHeight - 100)/(float)texture.height;
|
|
|
|
else imageScale = (float)(screenWidth - 100)/(float)texture.width;
|
|
|
|
}
|
2018-05-29 14:00:16 +02:00
|
|
|
}
|
2018-05-30 13:54:28 +02:00
|
|
|
|
2022-06-06 20:40:09 +02:00
|
|
|
UnloadDroppedFiles();
|
2018-05-30 13:54:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (btnExport)
|
|
|
|
{
|
|
|
|
if (imageLoaded)
|
2018-05-29 14:00:16 +02:00
|
|
|
{
|
2018-05-30 13:54:28 +02:00
|
|
|
ImageFormat(&image, pixelFormatActive + 1);
|
2018-05-29 14:00:16 +02:00
|
|
|
|
2018-05-30 13:54:28 +02:00
|
|
|
if (fileFormatActive == 0) // PNG
|
2018-05-29 14:00:16 +02:00
|
|
|
{
|
2021-03-12 18:01:14 +01:00
|
|
|
if ((GetFileExtension(fileName) == NULL) || (!IsFileExtension(fileName, ".png"))) strcat(fileName, ".png\0"); // No extension provided
|
2018-11-08 10:41:31 +01:00
|
|
|
ExportImage(image, fileName);
|
2018-05-29 14:00:16 +02:00
|
|
|
}
|
2018-05-30 13:54:28 +02:00
|
|
|
else if (fileFormatActive == 1) // RAW
|
2018-05-29 14:00:16 +02:00
|
|
|
{
|
2021-03-12 18:01:14 +01:00
|
|
|
if ((GetFileExtension(fileName) == NULL) || (!IsFileExtension(fileName, ".raw"))) strcat(fileName, ".raw\0"); // No extension provided
|
2018-05-30 13:54:28 +02:00
|
|
|
|
|
|
|
int dataSize = GetPixelDataSize(image.width, image.height, image.format);
|
2018-05-29 14:00:16 +02:00
|
|
|
|
2018-05-30 13:54:28 +02:00
|
|
|
FILE *rawFile = fopen(fileName, "wb");
|
|
|
|
fwrite(image.data, dataSize, 1, rawFile);
|
|
|
|
fclose(rawFile);
|
|
|
|
}
|
|
|
|
else if (fileFormatActive == 2) // CODE
|
|
|
|
{
|
2018-11-08 10:41:31 +01:00
|
|
|
ExportImageAsCode(image, fileName);
|
2018-05-29 14:00:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-30 13:54:28 +02:00
|
|
|
windowBoxActive = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (imageLoaded)
|
|
|
|
{
|
|
|
|
imageScale += (float)GetMouseWheelMove()*0.05f; // Image scale control
|
|
|
|
if (imageScale <= 0.1f) imageScale = 0.1f;
|
|
|
|
else if (imageScale >= 5) imageScale = 5;
|
2018-05-30 23:49:59 +02:00
|
|
|
|
2018-11-08 10:41:31 +01:00
|
|
|
imageRec = (Rectangle){ screenWidth/2 - (float)image.width*imageScale/2,
|
|
|
|
screenHeight/2 - (float)image.height*imageScale/2,
|
|
|
|
(float)image.width*imageScale, (float)image.height*imageScale };
|
2018-05-29 14:00:16 +02:00
|
|
|
}
|
2018-04-06 12:24:10 +02:00
|
|
|
//----------------------------------------------------------------------------------
|
2018-02-14 14:00:44 +01:00
|
|
|
|
|
|
|
// Draw
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
BeginDrawing();
|
|
|
|
|
|
|
|
ClearBackground(RAYWHITE);
|
2018-05-30 13:54:28 +02:00
|
|
|
|
|
|
|
if (texture.id > 0)
|
|
|
|
{
|
|
|
|
DrawTextureEx(texture, (Vector2){ screenWidth/2 - (float)texture.width*imageScale/2, screenHeight/2 - (float)texture.height*imageScale/2 }, 0.0f, imageScale, WHITE);
|
2018-05-30 23:49:59 +02:00
|
|
|
|
|
|
|
DrawRectangleLinesEx(imageRec, 1, CheckCollisionPointRec(GetMousePosition(), imageRec) ? RED : DARKGRAY);
|
2021-03-12 18:01:14 +01:00
|
|
|
DrawText(TextFormat("SCALE: %.2f%%", imageScale*100.0f), 20, screenHeight - 40, 20, GetColor(GuiGetStyle(DEFAULT, LINE_COLOR)));
|
2018-05-30 13:54:28 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-05-30 23:49:59 +02:00
|
|
|
DrawText("DRAG & DROP YOUR IMAGE!", 350, 200, 10, DARKGRAY);
|
2018-05-30 13:54:28 +02:00
|
|
|
GuiDisable();
|
|
|
|
}
|
2018-02-14 14:00:44 +01:00
|
|
|
|
2018-05-30 23:49:59 +02:00
|
|
|
if (GuiButton((Rectangle){ screenWidth - 170, screenHeight - 50, 150, 30 }, "Image Export")) windowBoxActive = true;
|
2018-05-30 13:54:28 +02:00
|
|
|
GuiEnable();
|
2018-02-14 14:00:44 +01:00
|
|
|
|
2018-04-27 14:14:04 +02:00
|
|
|
// Draw window box: windowBoxName
|
2018-04-06 12:24:10 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
if (windowBoxActive)
|
|
|
|
{
|
2019-07-05 20:47:15 +02:00
|
|
|
DrawRectangle(0, 0, screenWidth, screenHeight, Fade(GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR)), 0.7f));
|
2018-05-30 23:49:59 +02:00
|
|
|
windowBoxActive = !GuiWindowBox((Rectangle){ windowBoxRec.x, windowBoxRec.y, 220, 190 }, "Image Export Options");
|
2018-04-27 14:14:04 +02:00
|
|
|
|
2018-05-29 14:00:16 +02:00
|
|
|
GuiLabel((Rectangle){ windowBoxRec.x + 10, windowBoxRec.y + 35, 60, 25 }, "File format:");
|
2019-07-05 20:47:15 +02:00
|
|
|
fileFormatActive = GuiComboBox((Rectangle){ windowBoxRec.x + 80, windowBoxRec.y + 35, 130, 25 }, TextJoin(fileFormatTextList, 3, ";"), fileFormatActive);
|
2018-05-29 14:00:16 +02:00
|
|
|
GuiLabel((Rectangle){ windowBoxRec.x + 10, windowBoxRec.y + 70, 63, 25 }, "Pixel format:");
|
2019-07-05 20:47:15 +02:00
|
|
|
pixelFormatActive = GuiComboBox((Rectangle){ windowBoxRec.x + 80, windowBoxRec.y + 70, 130, 25 }, TextJoin(pixelFormatTextList, 7, ";"), pixelFormatActive);
|
2018-05-29 14:00:16 +02:00
|
|
|
GuiLabel((Rectangle){ windowBoxRec.x + 10, windowBoxRec.y + 105, 50, 25 }, "File name:");
|
2019-08-16 16:07:07 +02:00
|
|
|
if (GuiTextBox((Rectangle){ windowBoxRec.x + 80, windowBoxRec.y + 105, 130, 25 }, fileName, 64, textBoxEditMode)) textBoxEditMode = !textBoxEditMode;
|
|
|
|
|
2018-05-29 14:00:16 +02:00
|
|
|
btnExport = GuiButton((Rectangle){ windowBoxRec.x + 10, windowBoxRec.y + 145, 200, 30 }, "Export Image");
|
2018-04-06 12:24:10 +02:00
|
|
|
}
|
2018-05-30 13:54:28 +02:00
|
|
|
else btnExport = false;
|
2018-02-14 14:00:44 +01:00
|
|
|
|
2018-05-30 13:54:28 +02:00
|
|
|
if (btnExport) DrawText("Image exported!", 20, screenHeight - 20, 20, RED);
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2018-02-14 14:00:44 +01:00
|
|
|
EndDrawing();
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
}
|
|
|
|
|
|
|
|
// De-Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
2018-05-30 13:54:28 +02:00
|
|
|
UnloadImage(image);
|
|
|
|
UnloadTexture(texture);
|
|
|
|
|
2018-02-14 14:00:44 +01:00
|
|
|
CloseWindow(); // Close window and OpenGL context
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|