Image exporter: multiple tool improvements

- Created multiple functions to export to: .png, .raw and .h
- Image loading
- Image zoom
- Created resources folder with some images(for testing)
This commit is contained in:
Ray
2018-05-30 13:54:28 +02:00
parent 471c200b06
commit 1f1d4afdf0
5 changed files with 119 additions and 55 deletions

View File

@ -14,6 +14,14 @@
#define RAYGUI_IMPLEMENTATION
#include "raygui.h"
//#include "untitled.h"
// Static functions
static void ImageToCode(Image image, char *fileName); // Exports image to code (.h)
const char *pixelFormatTextList[7] = { "GRAYSCALE", "GRAY ALPHA", "R5G6B5", "R8G8B8", "R5G5B5A1", "R4G4B4A4", "R8G8B8A8" };
int pixelFormatActive = 0;
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
@ -21,36 +29,26 @@ int main(int argc, char *argv[0])
{
// Initialization
//--------------------------------------------------------------------------------------
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 450;
const int screenWidth = 800;
const int screenHeight = 450;
// Window box
Rectangle windowBoxRec = { SCREEN_WIDTH/2 - 130, SCREEN_HEIGHT/2 - 110, 260, 230 };
//SetConfigFlags();
InitWindow(screenWidth, screenHeight, "image exporter");
// Image export window variables
Rectangle windowBoxRec = { screenWidth/2 - 130, screenHeight/2 - 110, 260, 230 };
bool windowBoxActive = false;
// File format combo box
int formatCount = 3;
int fileFormatActive = 0;
const char *fileFormatTextList[3] = { "PNG", "RAW", "CODE"};
// Pixel format combo box
int pixelFormatCount = 6;
int pixelFormatActive = 0;
const char *pixelFormatTextList[6] = { "R8G8B8A8", "R8", "R5G5B5A1" ,"R5G6B5", "R8A8", "R4G4B4A4" };
// Text box
char fileName[32] = "Untitled";
const char *fileFormatTextList[3] = { "IMAGE (.png)", "DATA (.raw)", "CODE (.h)" };
char fileName[32] = "untitled";
//int dropdownSelected = 0;
SetConfigFlags(0x11100000);
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "image exporter");
int format = -1;
int pixelFormat = 0;
//Image image = { untitled_data, untitled_width, untitled_height, 1, untitled_format };
//Texture2D texture = LoadTextureFromImage(image);
Image image = { 0 };
Texture2D texture = { 0 };
bool imageLoaded = false;
bool btnExport = false;
Image image;
Texture2D texture;
float imageScale = 1.0f;
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
@ -66,35 +64,65 @@ int main(int argc, char *argv[0])
char **droppedFiles = GetDroppedFiles(&fileCount);
// Check file extensions for drag-and-drop
if ((fileCount == 1) && ((IsFileExtension(droppedFiles[0], ".raw") || IsFileExtension(droppedFiles[0], ".png"))))
if (fileCount == 1)
{
strcpy(fileName, GetFileName(droppedFiles[0]));
Image imTemp = LoadImage(droppedFiles[0]);
image = LoadImage(fileName);
}
if (btnExport)
{
format = fileFormatActive;
pixelFormat = pixelFormatActive;
//ImageFormat(image, pixelFormat);
if (format == 0) ExportImage(fileName, image);
else if (format == 1)
if (imTemp.data != NULL)
{
UnloadImage(image);
image = imTemp;
}
else if (format == 2)
{
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;
}
UnloadImage(image);
}
ClearDroppedFiles();
}
if (btnExport)
{
if (imageLoaded)
{
ImageFormat(&image, pixelFormatActive + 1);
if (fileFormatActive == 0) // PNG
{
if ((GetExtension(fileName) == NULL) || (!IsFileExtension(fileName, ".png"))) strcat(fileName, ".png\0"); // No extension provided
ExportImage(fileName, image);
}
else if (fileFormatActive == 1) // RAW
{
if ((GetExtension(fileName) == NULL) || (!IsFileExtension(fileName, ".raw"))) strcat(fileName, ".raw\0"); // No extension provided
int dataSize = GetPixelDataSize(image.width, image.height, image.format);
FILE *rawFile = fopen(fileName, "wb");
fwrite(image.data, dataSize, 1, rawFile);
fclose(rawFile);
}
else if (fileFormatActive == 2) // CODE
{
ImageToCode(image, fileName);
}
}
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;
}
//----------------------------------------------------------------------------------
// Draw
@ -102,41 +130,77 @@ int main(int argc, char *argv[0])
BeginDrawing();
ClearBackground(RAYWHITE);
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);
DrawText(FormatText("SCALE: %.2f%%", imageScale*100.0f), 20, screenHeight - 40, 20, GetColor(style[DEFAULT_LINES_COLOR]));
}
else
{
DrawText("Drag & drop your image!", 300, 200, 10, DARKGRAY);
GuiDisable();
}
// Draw message box (testing)
//GuiMessageBox((Rectangle){600, 100, 150, 100}, "Image export options", "Hello World!\nHello World!");
DrawText("Drag & drop your image!", 300, 200, 10, DARKGRAY);
if (GuiButton((Rectangle){ 20, 20, 150, 30 }, "Show Export Window")) windowBoxActive = true;
if (GuiButton((Rectangle){ screenWidth - 170, screenHeight - 50, 150, 30 }, "Show Export Window")) windowBoxActive = true;
GuiEnable();
// Draw window box: windowBoxName
//-----------------------------------------------------------------------------
if (windowBoxActive)
{
DrawRectangle(0,0, screenWidth, screenHeight, Fade(GetColor(style[DEFAULT_BACKGROUND_COLOR]), 0.7f));
windowBoxActive = !GuiWindowBox((Rectangle){ windowBoxRec.x + 0, windowBoxRec.y + 0, 220, 190 }, "Image Export Options");
GuiLabel((Rectangle){ windowBoxRec.x + 10, windowBoxRec.y + 35, 60, 25 }, "File format:");
fileFormatActive = GuiComboBox((Rectangle){ windowBoxRec.x + 80, windowBoxRec.y + 35, 130, 25 }, fileFormatTextList, 3, fileFormatActive);
GuiLabel((Rectangle){ windowBoxRec.x + 10, windowBoxRec.y + 70, 63, 25 }, "Pixel format:");
pixelFormatActive = GuiComboBox((Rectangle){ windowBoxRec.x + 80, windowBoxRec.y + 70, 130, 25 }, pixelFormatTextList, 6, pixelFormatActive);
pixelFormatActive = GuiComboBox((Rectangle){ windowBoxRec.x + 80, windowBoxRec.y + 70, 130, 25 }, pixelFormatTextList, 7, pixelFormatActive);
GuiLabel((Rectangle){ windowBoxRec.x + 10, windowBoxRec.y + 105, 50, 25 }, "File name:");
GuiTextBox((Rectangle){ windowBoxRec.x + 80, windowBoxRec.y + 105, 130, 25 }, fileName, 64, true);
btnExport = GuiButton((Rectangle){ windowBoxRec.x + 10, windowBoxRec.y + 145, 200, 30 }, "Export Image");
}
else btnExport = false;
if (btnExport) DrawText("Image exported!", 20, screenHeight - 20, 20, RED);
//-----------------------------------------------------------------------------
// Draw dropdown box (testing)
//dropdownSelected = GuiDropdownBox((Rectangle){ 10, 10, 150, 30 }, formatText, formatCount, dropdownSelected);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadImage(image);
UnloadTexture(texture);
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
static void ImageToCode(Image image, char *fileName)
{
char outName[32] = "\0";
strcpy(outName, fileName);
if ((GetExtension(fileName) == NULL) || (!IsFileExtension(fileName, ".h"))) strcat(fileName, ".h\0"); // No extension provided
int dataSize = GetPixelDataSize(image.width, image.height, image.format);
FILE *txtFile = fopen(fileName, "wt");
// TODO: Add image information
fprintf(txtFile, "//\n// Image exported using raygui image_exporter example\n//\n// Copyright (c) 2018 raylib technologies (@raylibtech)\n//\n\n");
fprintf(txtFile, "#define %s_width %i\n", outName, image.width);
fprintf(txtFile, "#define %s_height %i\n", outName, image.height);
fprintf(txtFile, "#define %s_format %i // raylib internal pixel format: %s\n\n", outName, pixelFormatActive + 1, pixelFormatTextList[pixelFormatActive]);
fprintf(txtFile, "static unsigned char %s_data[%i] = { ", outName, dataSize);
for (int i = 0; i < dataSize - 1; i++) fprintf(txtFile, "0x%x, ", ((unsigned char *)image.data)[i]);
fprintf(txtFile, "0x%x };\n", ((unsigned char *)image.data)[dataSize - 1]);
fclose(txtFile);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 379 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 218 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 288 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB