image_exporter.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*******************************************************************************************
  2. *
  3. * raygui - image exporter
  4. *
  5. * DEPENDENCIES:
  6. * raylib 4.0 - Windowing/input management and drawing.
  7. * raygui 3.0 - Immediate-mode GUI controls.
  8. *
  9. * COMPILATION (Windows - MinGW):
  10. * gcc -o $(NAME_PART).exe $(FILE_NAME) -I../../src -lraylib -lopengl32 -lgdi32 -std=c99
  11. *
  12. * LICENSE: zlib/libpng
  13. *
  14. * Copyright (c) 2015-2024 Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #define RAYGUI_IMPLEMENTATION
  19. #include "../../src/raygui.h"
  20. //------------------------------------------------------------------------------------
  21. // Program main entry point
  22. //------------------------------------------------------------------------------------
  23. int main(int argc, char *argv[])
  24. {
  25. // Initialization
  26. //--------------------------------------------------------------------------------------
  27. const int screenWidth = 800;
  28. const int screenHeight = 450;
  29. InitWindow(screenWidth, screenHeight, "raygui - image exporter");
  30. // GUI controls initialization
  31. //----------------------------------------------------------------------------------
  32. Rectangle windowBoxRec = { screenWidth/2 - 110, screenHeight/2 - 100, 220, 190 };
  33. bool windowBoxActive = false;
  34. int fileFormatActive = 0;
  35. const char *fileFormatTextList[3] = { "IMAGE (.png)", "DATA (.raw)", "CODE (.h)" };
  36. int pixelFormatActive = 0;
  37. const char *pixelFormatTextList[7] = { "GRAYSCALE", "GRAY ALPHA", "R5G6B5", "R8G8B8", "R5G5B5A1", "R4G4B4A4", "R8G8B8A8" };
  38. bool textBoxEditMode = false;
  39. char fileName[64] = "untitled";
  40. //--------------------------------------------------------------------------------------
  41. Image image = { 0 };
  42. Texture2D texture = { 0 };
  43. bool imageLoaded = false;
  44. float imageScale = 1.0f;
  45. Rectangle imageRec = { 0 };
  46. bool btnExportPressed = false;
  47. SetTargetFPS(60);
  48. //--------------------------------------------------------------------------------------
  49. // Main game loop
  50. while (!WindowShouldClose()) // Detect window close button or ESC key
  51. {
  52. // Update
  53. //----------------------------------------------------------------------------------
  54. if (IsFileDropped())
  55. {
  56. FilePathList droppedFiles = LoadDroppedFiles();
  57. if (droppedFiles.count == 1)
  58. {
  59. Image imTemp = LoadImage(droppedFiles.paths[0]);
  60. if (imTemp.data != NULL)
  61. {
  62. UnloadImage(image);
  63. image = imTemp;
  64. UnloadTexture(texture);
  65. texture = LoadTextureFromImage(image);
  66. imageLoaded = true;
  67. pixelFormatActive = image.format - 1;
  68. if (texture.height > texture.width) imageScale = (float)(screenHeight - 100)/(float)texture.height;
  69. else imageScale = (float)(screenWidth - 100)/(float)texture.width;
  70. }
  71. }
  72. UnloadDroppedFiles(droppedFiles);
  73. }
  74. if (btnExportPressed)
  75. {
  76. if (imageLoaded)
  77. {
  78. ImageFormat(&image, pixelFormatActive + 1);
  79. if (fileFormatActive == 0) // PNG
  80. {
  81. if ((GetFileExtension(fileName) == NULL) || (!IsFileExtension(fileName, ".png"))) strcat(fileName, ".png\0"); // No extension provided
  82. ExportImage(image, fileName);
  83. }
  84. else if (fileFormatActive == 1) // RAW
  85. {
  86. if ((GetFileExtension(fileName) == NULL) || (!IsFileExtension(fileName, ".raw"))) strcat(fileName, ".raw\0"); // No extension provided
  87. int dataSize = GetPixelDataSize(image.width, image.height, image.format);
  88. FILE *rawFile = fopen(fileName, "wb");
  89. fwrite(image.data, 1, dataSize, rawFile);
  90. fclose(rawFile);
  91. }
  92. else if (fileFormatActive == 2) // CODE
  93. {
  94. ExportImageAsCode(image, fileName);
  95. }
  96. }
  97. windowBoxActive = false;
  98. }
  99. if (imageLoaded)
  100. {
  101. imageScale += (float)GetMouseWheelMove()*0.05f; // Image scale control
  102. if (imageScale <= 0.1f) imageScale = 0.1f;
  103. else if (imageScale >= 5) imageScale = 5;
  104. imageRec = (Rectangle){ screenWidth/2 - (float)image.width*imageScale/2,
  105. screenHeight/2 - (float)image.height*imageScale/2,
  106. (float)image.width*imageScale, (float)image.height*imageScale };
  107. }
  108. //----------------------------------------------------------------------------------
  109. // Draw
  110. //----------------------------------------------------------------------------------
  111. BeginDrawing();
  112. ClearBackground(RAYWHITE);
  113. if (texture.id > 0)
  114. {
  115. DrawTextureEx(texture, (Vector2){ screenWidth/2 - (float)texture.width*imageScale/2, screenHeight/2 - (float)texture.height*imageScale/2 }, 0.0f, imageScale, WHITE);
  116. DrawRectangleLinesEx(imageRec, 1, CheckCollisionPointRec(GetMousePosition(), imageRec) ? RED : DARKGRAY);
  117. DrawText(TextFormat("SCALE: %.2f%%", imageScale*100.0f), 20, screenHeight - 40, 20, GetColor(GuiGetStyle(DEFAULT, LINE_COLOR)));
  118. }
  119. else
  120. {
  121. DrawText("DRAG & DROP YOUR IMAGE!", 350, 200, 10, DARKGRAY);
  122. GuiDisable();
  123. }
  124. if (GuiButton((Rectangle){ screenWidth - 170, screenHeight - 50, 150, 30 }, "Image Export")) windowBoxActive = true;
  125. GuiEnable();
  126. // Draw window box: windowBoxName
  127. //-----------------------------------------------------------------------------
  128. if (windowBoxActive)
  129. {
  130. DrawRectangle(0, 0, screenWidth, screenHeight, Fade(GetColor(GuiGetStyle(DEFAULT, BACKGROUND_COLOR)), 0.7f));
  131. windowBoxActive = !GuiWindowBox((Rectangle){ windowBoxRec.x, windowBoxRec.y, 220, 190 }, "Image Export Options");
  132. GuiLabel((Rectangle){ windowBoxRec.x + 10, windowBoxRec.y + 35, 60, 25 }, "File format:");
  133. GuiComboBox((Rectangle){ windowBoxRec.x + 80, windowBoxRec.y + 35, 130, 25 }, TextJoin(fileFormatTextList, 3, ";"), &fileFormatActive);
  134. GuiLabel((Rectangle){ windowBoxRec.x + 10, windowBoxRec.y + 70, 63, 25 }, "Pixel format:");
  135. GuiComboBox((Rectangle){ windowBoxRec.x + 80, windowBoxRec.y + 70, 130, 25 }, TextJoin(pixelFormatTextList, 7, ";"), &pixelFormatActive);
  136. GuiLabel((Rectangle){ windowBoxRec.x + 10, windowBoxRec.y + 105, 50, 25 }, "File name:");
  137. if (GuiTextBox((Rectangle){ windowBoxRec.x + 80, windowBoxRec.y + 105, 130, 25 }, fileName, 64, textBoxEditMode)) textBoxEditMode = !textBoxEditMode;
  138. btnExportPressed = GuiButton((Rectangle){ windowBoxRec.x + 10, windowBoxRec.y + 145, 200, 30 }, "Export Image");
  139. }
  140. else btnExportPressed = false;
  141. if (btnExportPressed) DrawText("Image exported!", 20, screenHeight - 20, 20, RED);
  142. //-----------------------------------------------------------------------------
  143. EndDrawing();
  144. //----------------------------------------------------------------------------------
  145. }
  146. // De-Initialization
  147. //--------------------------------------------------------------------------------------
  148. UnloadImage(image);
  149. UnloadTexture(texture);
  150. CloseWindow(); // Close window and OpenGL context
  151. //--------------------------------------------------------------------------------------
  152. return 0;
  153. }