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.

178 regels
6.9 KiB

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