選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

130 行
4.5 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - Image loading and texture creation
  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.3, last time updated with raylib 1.3
  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) 2015-2024 Karim Salem (@kimo-s)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. //------------------------------------------------------------------------------------
  17. // Program main entry point
  18. //------------------------------------------------------------------------------------
  19. void NormalizeKernel(float *kernel, int size)
  20. {
  21. float sum = 0.0f;
  22. for (int i = 0; i < size; i++) sum += kernel[i];
  23. if (sum != 0.0f)
  24. {
  25. for (int i = 0; i < size; i++) kernel[i] /= sum;
  26. }
  27. }
  28. int main(void)
  29. {
  30. // Initialization
  31. //--------------------------------------------------------------------------------------
  32. const int screenWidth = 800;
  33. const int screenHeight = 450;
  34. InitWindow(screenWidth, screenHeight, "raylib [textures] example - image convolution");
  35. Image image = LoadImage("resources/cat.png"); // Loaded in CPU memory (RAM)
  36. float gaussiankernel[] = {
  37. 1.0f, 2.0f, 1.0f,
  38. 2.0f, 4.0f, 2.0f,
  39. 1.0f, 2.0f, 1.0f };
  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. float sharpenkernel[] = {
  45. 0.0f, -1.0f, 0.0f,
  46. -1.0f, 5.0f, -1.0f,
  47. 0.0f, -1.0f, 0.0f };
  48. NormalizeKernel(gaussiankernel, 9);
  49. NormalizeKernel(sharpenkernel, 9);
  50. NormalizeKernel(sobelkernel, 9);
  51. Image catSharpend = ImageCopy(image);
  52. ImageKernelConvolution(&catSharpend, sharpenkernel, 9);
  53. Image catSobel = ImageCopy(image);
  54. ImageKernelConvolution(&catSobel, sobelkernel, 9);
  55. Image catGaussian = ImageCopy(image);
  56. for (int i = 0; i < 6; i++)
  57. {
  58. ImageKernelConvolution(&catGaussian, gaussiankernel, 9);
  59. }
  60. ImageCrop(&image, (Rectangle){ 0, 0, (float)200, (float)450 });
  61. ImageCrop(&catGaussian, (Rectangle){ 0, 0, (float)200, (float)450 });
  62. ImageCrop(&catSobel, (Rectangle){ 0, 0, (float)200, (float)450 });
  63. ImageCrop(&catSharpend, (Rectangle){ 0, 0, (float)200, (float)450 });
  64. // Images converted to texture, GPU memory (VRAM)
  65. Texture2D texture = LoadTextureFromImage(image);
  66. Texture2D catSharpendTexture = LoadTextureFromImage(catSharpend);
  67. Texture2D catSobelTexture = LoadTextureFromImage(catSobel);
  68. Texture2D catGaussianTexture = LoadTextureFromImage(catGaussian);
  69. // Once images have been converted to texture and uploaded to VRAM,
  70. // they can be unloaded from RAM
  71. UnloadImage(image);
  72. UnloadImage(catGaussian);
  73. UnloadImage(catSobel);
  74. UnloadImage(catSharpend);
  75. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  76. //---------------------------------------------------------------------------------------
  77. // Main game loop
  78. while (!WindowShouldClose()) // Detect window close button or ESC key
  79. {
  80. // Update
  81. //----------------------------------------------------------------------------------
  82. // TODO: Update your variables here
  83. //----------------------------------------------------------------------------------
  84. // Draw
  85. //----------------------------------------------------------------------------------
  86. BeginDrawing();
  87. ClearBackground(RAYWHITE);
  88. DrawTexture(catSharpendTexture, 0, 0, WHITE);
  89. DrawTexture(catSobelTexture, 200, 0, WHITE);
  90. DrawTexture(catGaussianTexture, 400, 0, WHITE);
  91. DrawTexture(texture, 600, 0, WHITE);
  92. EndDrawing();
  93. //----------------------------------------------------------------------------------
  94. }
  95. // De-Initialization
  96. //--------------------------------------------------------------------------------------
  97. UnloadTexture(texture);
  98. UnloadTexture(catGaussianTexture);
  99. UnloadTexture(catSobelTexture);
  100. UnloadTexture(catSharpendTexture);
  101. CloseWindow(); // Close window and OpenGL context
  102. //--------------------------------------------------------------------------------------
  103. return 0;
  104. }