You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
6.4 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - Image processing
  4. *
  5. * NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
  6. *
  7. * This example has been created using raylib 3.5 (www.raylib.com)
  8. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  9. *
  10. * Copyright (c) 2016 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #include <stdlib.h> // Required for: free()
  15. #define NUM_PROCESSES 8
  16. typedef enum {
  17. NONE = 0,
  18. COLOR_GRAYSCALE,
  19. COLOR_TINT,
  20. COLOR_INVERT,
  21. COLOR_CONTRAST,
  22. COLOR_BRIGHTNESS,
  23. FLIP_VERTICAL,
  24. FLIP_HORIZONTAL
  25. } ImageProcess;
  26. static const char *processText[] = {
  27. "NO PROCESSING",
  28. "COLOR GRAYSCALE",
  29. "COLOR TINT",
  30. "COLOR INVERT",
  31. "COLOR CONTRAST",
  32. "COLOR BRIGHTNESS",
  33. "FLIP VERTICAL",
  34. "FLIP HORIZONTAL"
  35. };
  36. int main(void)
  37. {
  38. // Initialization
  39. //--------------------------------------------------------------------------------------
  40. const int screenWidth = 800;
  41. const int screenHeight = 450;
  42. InitWindow(screenWidth, screenHeight, "raylib [textures] example - image processing");
  43. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  44. Image imOrigin = LoadImage("resources/parrots.png"); // Loaded in CPU memory (RAM)
  45. ImageFormat(&imOrigin, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8); // Format image to RGBA 32bit (required for texture update) <-- ISSUE
  46. Texture2D texture = LoadTextureFromImage(imOrigin); // Image converted to texture, GPU memory (VRAM)
  47. Image imCopy = ImageCopy(imOrigin);
  48. int currentProcess = NONE;
  49. bool textureReload = false;
  50. Rectangle toggleRecs[NUM_PROCESSES] = { 0 };
  51. int mouseHoverRec = -1;
  52. for (int i = 0; i < NUM_PROCESSES; i++) toggleRecs[i] = (Rectangle){ 40.0f, (float)(50 + 32*i), 150.0f, 30.0f };
  53. SetTargetFPS(60);
  54. //---------------------------------------------------------------------------------------
  55. // Main game loop
  56. while (!WindowShouldClose()) // Detect window close button or ESC key
  57. {
  58. // Update
  59. //----------------------------------------------------------------------------------
  60. // Mouse toggle group logic
  61. for (int i = 0; i < NUM_PROCESSES; i++)
  62. {
  63. if (CheckCollisionPointRec(GetMousePosition(), toggleRecs[i]))
  64. {
  65. mouseHoverRec = i;
  66. if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON))
  67. {
  68. currentProcess = i;
  69. textureReload = true;
  70. }
  71. break;
  72. }
  73. else mouseHoverRec = -1;
  74. }
  75. // Keyboard toggle group logic
  76. if (IsKeyPressed(KEY_DOWN))
  77. {
  78. currentProcess++;
  79. if (currentProcess > (NUM_PROCESSES - 1)) currentProcess = 0;
  80. textureReload = true;
  81. }
  82. else if (IsKeyPressed(KEY_UP))
  83. {
  84. currentProcess--;
  85. if (currentProcess < 0) currentProcess = 7;
  86. textureReload = true;
  87. }
  88. // Reload texture when required
  89. if (textureReload)
  90. {
  91. UnloadImage(imCopy); // Unload image-copy data
  92. imCopy = ImageCopy(imOrigin); // Restore image-copy from image-origin
  93. // NOTE: Image processing is a costly CPU process to be done every frame,
  94. // If image processing is required in a frame-basis, it should be done
  95. // with a texture and by shaders
  96. switch (currentProcess)
  97. {
  98. case COLOR_GRAYSCALE: ImageColorGrayscale(&imCopy); break;
  99. case COLOR_TINT: ImageColorTint(&imCopy, GREEN); break;
  100. case COLOR_INVERT: ImageColorInvert(&imCopy); break;
  101. case COLOR_CONTRAST: ImageColorContrast(&imCopy, -40); break;
  102. case COLOR_BRIGHTNESS: ImageColorBrightness(&imCopy, -80); break;
  103. case FLIP_VERTICAL: ImageFlipVertical(&imCopy); break;
  104. case FLIP_HORIZONTAL: ImageFlipHorizontal(&imCopy); break;
  105. default: break;
  106. }
  107. Color *pixels = LoadImageColors(imCopy); // Load pixel data from image (RGBA 32bit)
  108. UpdateTexture(texture, pixels); // Update texture with new image data
  109. UnloadImageColors(pixels); // Unload pixels data from RAM
  110. textureReload = false;
  111. }
  112. //----------------------------------------------------------------------------------
  113. // Draw
  114. //----------------------------------------------------------------------------------
  115. BeginDrawing();
  116. ClearBackground(RAYWHITE);
  117. DrawText("IMAGE PROCESSING:", 40, 30, 10, DARKGRAY);
  118. // Draw rectangles
  119. for (int i = 0; i < NUM_PROCESSES; i++)
  120. {
  121. DrawRectangleRec(toggleRecs[i], ((i == currentProcess) || (i == mouseHoverRec)) ? SKYBLUE : LIGHTGRAY);
  122. DrawRectangleLines((int)toggleRecs[i].x, (int) toggleRecs[i].y, (int) toggleRecs[i].width, (int) toggleRecs[i].height, ((i == currentProcess) || (i == mouseHoverRec)) ? BLUE : GRAY);
  123. DrawText( processText[i], (int)( toggleRecs[i].x + toggleRecs[i].width/2 - MeasureText(processText[i], 10)/2), (int) toggleRecs[i].y + 11, 10, ((i == currentProcess) || (i == mouseHoverRec)) ? DARKBLUE : DARKGRAY);
  124. }
  125. DrawTexture(texture, screenWidth - texture.width - 60, screenHeight/2 - texture.height/2, WHITE);
  126. DrawRectangleLines(screenWidth - texture.width - 60, screenHeight/2 - texture.height/2, texture.width, texture.height, BLACK);
  127. EndDrawing();
  128. //----------------------------------------------------------------------------------
  129. }
  130. // De-Initialization
  131. //--------------------------------------------------------------------------------------
  132. UnloadTexture(texture); // Unload texture from VRAM
  133. UnloadImage(imOrigin); // Unload image-origin from RAM
  134. UnloadImage(imCopy); // Unload image-copy from RAM
  135. CloseWindow(); // Close window and OpenGL context
  136. //--------------------------------------------------------------------------------------
  137. return 0;
  138. }