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.

153 lines
6.0 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()
  37. {
  38. // Initialization
  39. //--------------------------------------------------------------------------------------
  40. int screenWidth = 800;
  41. 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)
  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];
  50. for (int i = 0; i < NUM_PROCESSES; i++) selectRecs[i] = (Rectangle){ 40, 50 + 32*i, 150, 30 };
  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. if (i == currentProcess)
  103. {
  104. DrawRectangleRec(selectRecs[i], SKYBLUE);
  105. DrawRectangleLines(selectRecs[i].x, selectRecs[i].y, selectRecs[i].width, selectRecs[i].height, BLUE);
  106. DrawText(processText[i], selectRecs[i].x + selectRecs[i].width/2 - MeasureText(processText[i], 10)/2, selectRecs[i].y + 11, 10, DARKBLUE);
  107. }
  108. else
  109. {
  110. DrawRectangleRec(selectRecs[i], LIGHTGRAY);
  111. DrawRectangleLines(selectRecs[i].x, selectRecs[i].y, selectRecs[i].width, selectRecs[i].height, GRAY);
  112. DrawText(processText[i], selectRecs[i].x + selectRecs[i].width/2 - MeasureText(processText[i], 10)/2, selectRecs[i].y + 11, 10, DARKGRAY);
  113. }
  114. }
  115. DrawTexture(texture, screenWidth - texture.width - 60, screenHeight/2 - texture.height/2, WHITE);
  116. DrawRectangleLines(screenWidth - texture.width - 60, screenHeight/2 - texture.height/2, texture.width, texture.height, BLACK);
  117. EndDrawing();
  118. //----------------------------------------------------------------------------------
  119. }
  120. // De-Initialization
  121. //--------------------------------------------------------------------------------------
  122. UnloadTexture(texture); // Unload texture from VRAM
  123. UnloadImage(image); // Unload image from RAM
  124. CloseWindow(); // Close window and OpenGL context
  125. //--------------------------------------------------------------------------------------
  126. return 0;
  127. }