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.

176 rivejä
6.8 KiB

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