Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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