textures_image_kernel.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - image kernel
  4. *
  5. * Example complexity rating: [★★★★] 4/4
  6. *
  7. * NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
  8. *
  9. * Example contributed by Karim Salem (@kimo-s) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example originally created with raylib 1.3, last time updated with raylib 1.3
  12. *
  13. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  14. * BSD-like license that allows static linking with closed source software
  15. *
  16. * Copyright (c) 2015-2025 Karim Salem (@kimo-s)
  17. *
  18. ********************************************************************************************/
  19. #include "raylib.h"
  20. //------------------------------------------------------------------------------------
  21. // Module Functions Declaration
  22. //------------------------------------------------------------------------------------
  23. static void NormalizeKernel(float *kernel, int size);
  24. //------------------------------------------------------------------------------------
  25. // Program main entry point
  26. //------------------------------------------------------------------------------------
  27. int main(void)
  28. {
  29. // Initialization
  30. //--------------------------------------------------------------------------------------
  31. const int screenWidth = 800;
  32. const int screenHeight = 450;
  33. InitWindow(screenWidth, screenHeight, "raylib [textures] example - image kernel");
  34. Image image = LoadImage("resources/cat.png"); // Loaded in CPU memory (RAM)
  35. float gaussiankernel[] = {
  36. 1.0f, 2.0f, 1.0f,
  37. 2.0f, 4.0f, 2.0f,
  38. 1.0f, 2.0f, 1.0f
  39. };
  40. float sobelkernel[] = {
  41. 1.0f, 0.0f, -1.0f,
  42. 2.0f, 0.0f, -2.0f,
  43. 1.0f, 0.0f, -1.0f
  44. };
  45. float sharpenkernel[] = {
  46. 0.0f, -1.0f, 0.0f,
  47. -1.0f, 5.0f, -1.0f,
  48. 0.0f, -1.0f, 0.0f
  49. };
  50. NormalizeKernel(gaussiankernel, 9);
  51. NormalizeKernel(sharpenkernel, 9);
  52. NormalizeKernel(sobelkernel, 9);
  53. Image catSharpend = ImageCopy(image);
  54. ImageKernelConvolution(&catSharpend, sharpenkernel, 9);
  55. Image catSobel = ImageCopy(image);
  56. ImageKernelConvolution(&catSobel, sobelkernel, 9);
  57. Image catGaussian = ImageCopy(image);
  58. for (int i = 0; i < 6; i++)
  59. {
  60. ImageKernelConvolution(&catGaussian, gaussiankernel, 9);
  61. }
  62. ImageCrop(&image, (Rectangle){ 0, 0, (float)200, (float)450 });
  63. ImageCrop(&catGaussian, (Rectangle){ 0, 0, (float)200, (float)450 });
  64. ImageCrop(&catSobel, (Rectangle){ 0, 0, (float)200, (float)450 });
  65. ImageCrop(&catSharpend, (Rectangle){ 0, 0, (float)200, (float)450 });
  66. // Images converted to texture, GPU memory (VRAM)
  67. Texture2D texture = LoadTextureFromImage(image);
  68. Texture2D catSharpendTexture = LoadTextureFromImage(catSharpend);
  69. Texture2D catSobelTexture = LoadTextureFromImage(catSobel);
  70. Texture2D catGaussianTexture = LoadTextureFromImage(catGaussian);
  71. // Once images have been converted to texture and uploaded to VRAM,
  72. // they can be unloaded from RAM
  73. UnloadImage(image);
  74. UnloadImage(catGaussian);
  75. UnloadImage(catSobel);
  76. UnloadImage(catSharpend);
  77. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  78. //---------------------------------------------------------------------------------------
  79. // Main game loop
  80. while (!WindowShouldClose()) // Detect window close button or ESC key
  81. {
  82. // Update
  83. //----------------------------------------------------------------------------------
  84. // TODO: Update your variables here
  85. //----------------------------------------------------------------------------------
  86. // Draw
  87. //----------------------------------------------------------------------------------
  88. BeginDrawing();
  89. ClearBackground(RAYWHITE);
  90. DrawTexture(catSharpendTexture, 0, 0, WHITE);
  91. DrawTexture(catSobelTexture, 200, 0, WHITE);
  92. DrawTexture(catGaussianTexture, 400, 0, WHITE);
  93. DrawTexture(texture, 600, 0, WHITE);
  94. EndDrawing();
  95. //----------------------------------------------------------------------------------
  96. }
  97. // De-Initialization
  98. //--------------------------------------------------------------------------------------
  99. UnloadTexture(texture);
  100. UnloadTexture(catGaussianTexture);
  101. UnloadTexture(catSobelTexture);
  102. UnloadTexture(catSharpendTexture);
  103. CloseWindow(); // Close window and OpenGL context
  104. //--------------------------------------------------------------------------------------
  105. return 0;
  106. }
  107. //------------------------------------------------------------------------------------
  108. // Module Functions Definition
  109. //------------------------------------------------------------------------------------
  110. static void NormalizeKernel(float *kernel, int size)
  111. {
  112. float sum = 0.0f;
  113. for (int i = 0; i < size; i++) sum += kernel[i];
  114. if (sum != 0.0f)
  115. {
  116. for (int i = 0; i < size; i++) kernel[i] /= sum;
  117. }
  118. }