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.

144 lines
5.6 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 1.4 (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 image = LoadImage("resources/parrots.png"); // Loaded in CPU memory (RAM)
  45. ImageFormat(&image, UNCOMPRESSED_R8G8B8A8); // Format image to RGBA 32bit (required for texture update) <-- ISSUE
  46. Texture2D texture = LoadTextureFromImage(image); // Image converted to texture, GPU memory (VRAM)
  47. int currentProcess = NONE;
  48. bool textureReload = false;
  49. Rectangle selectRecs[NUM_PROCESSES] = { 0 };
  50. for (int i = 0; i < NUM_PROCESSES; i++) selectRecs[i] = (Rectangle){ 40.0f, (float)(50 + 32*i), 150.0f, 30.0f };
  51. SetTargetFPS(60);
  52. //---------------------------------------------------------------------------------------
  53. // Main game loop
  54. while (!WindowShouldClose()) // Detect window close button or ESC key
  55. {
  56. // Update
  57. //----------------------------------------------------------------------------------
  58. if (IsKeyPressed(KEY_DOWN))
  59. {
  60. currentProcess++;
  61. if (currentProcess > 7) currentProcess = 0;
  62. textureReload = true;
  63. }
  64. else if (IsKeyPressed(KEY_UP))
  65. {
  66. currentProcess--;
  67. if (currentProcess < 0) currentProcess = 7;
  68. textureReload = true;
  69. }
  70. if (textureReload)
  71. {
  72. UnloadImage(image); // Unload current image data
  73. image = LoadImage("resources/parrots.png"); // Re-load image data
  74. // NOTE: Image processing is a costly CPU process to be done every frame,
  75. // If image processing is required in a frame-basis, it should be done
  76. // with a texture and by shaders
  77. switch (currentProcess)
  78. {
  79. case COLOR_GRAYSCALE: ImageColorGrayscale(&image); break;
  80. case COLOR_TINT: ImageColorTint(&image, GREEN); break;
  81. case COLOR_INVERT: ImageColorInvert(&image); break;
  82. case COLOR_CONTRAST: ImageColorContrast(&image, -40); break;
  83. case COLOR_BRIGHTNESS: ImageColorBrightness(&image, -80); break;
  84. case FLIP_VERTICAL: ImageFlipVertical(&image); break;
  85. case FLIP_HORIZONTAL: ImageFlipHorizontal(&image); break;
  86. default: break;
  87. }
  88. Color *pixels = GetImageData(image); // Get pixel data from image (RGBA 32bit)
  89. UpdateTexture(texture, pixels); // Update texture with new image data
  90. free(pixels); // Unload pixels data from RAM
  91. textureReload = false;
  92. }
  93. //----------------------------------------------------------------------------------
  94. // Draw
  95. //----------------------------------------------------------------------------------
  96. BeginDrawing();
  97. ClearBackground(RAYWHITE);
  98. DrawText("IMAGE PROCESSING:", 40, 30, 10, DARKGRAY);
  99. // Draw rectangles
  100. for (int i = 0; i < NUM_PROCESSES; i++)
  101. {
  102. DrawRectangleRec(selectRecs[i], (i == currentProcess) ? SKYBLUE : LIGHTGRAY);
  103. DrawRectangleLines((int)selectRecs[i].x, (int) selectRecs[i].y, (int) selectRecs[i].width, (int) selectRecs[i].height, (i == currentProcess) ? BLUE : GRAY);
  104. DrawText( processText[i], (int)( selectRecs[i].x + selectRecs[i].width/2 - MeasureText(processText[i], 10)/2), (int) selectRecs[i].y + 11, 10, (i == currentProcess) ? DARKBLUE : DARKGRAY);
  105. }
  106. DrawTexture(texture, screenWidth - texture.width - 60, screenHeight/2 - texture.height/2, WHITE);
  107. DrawRectangleLines(screenWidth - texture.width - 60, screenHeight/2 - texture.height/2, texture.width, texture.height, BLACK);
  108. EndDrawing();
  109. //----------------------------------------------------------------------------------
  110. }
  111. // De-Initialization
  112. //--------------------------------------------------------------------------------------
  113. UnloadTexture(texture); // Unload texture from VRAM
  114. UnloadImage(image); // Unload image from RAM
  115. CloseWindow(); // Close window and OpenGL context
  116. //--------------------------------------------------------------------------------------
  117. return 0;
  118. }