Update textures_image_kernel.c

This commit is contained in:
Ray
2025-08-07 17:08:02 +02:00
parent f02c7fca8a
commit 9f07cfe0b7

View File

@@ -1,6 +1,6 @@
/*******************************************************************************************
*
* raylib [textures] example - Image loading and texture creation
* raylib [textures] example - image kernel convolution
*
* Example complexity rating: [★★★★] 4/4
*
@@ -19,20 +19,14 @@
#include "raylib.h"
//------------------------------------------------------------------------------------
// Module functions declaration
//------------------------------------------------------------------------------------
void NormalizeKernel(float *kernel, int size);
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
void NormalizeKernel(float *kernel, int size)
{
float sum = 0.0f;
for (int i = 0; i < size; i++) sum += kernel[i];
if (sum != 0.0f)
{
for (int i = 0; i < size; i++) kernel[i] /= sum;
}
}
int main(void)
{
// Initialization
@@ -41,23 +35,26 @@ int main(void)
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [textures] example - image convolution");
Image image = LoadImage("resources/cat.png"); // Loaded in CPU memory (RAM)
float gaussiankernel[] = {
Image image = LoadImage("resources/cat.png"); // Loaded in CPU memory (RAM)
float gaussiankernel[] = {
1.0f, 2.0f, 1.0f,
2.0f, 4.0f, 2.0f,
1.0f, 2.0f, 1.0f };
1.0f, 2.0f, 1.0f
};
float sobelkernel[] = {
1.0f, 0.0f, -1.0f,
2.0f, 0.0f, -2.0f,
1.0f, 0.0f, -1.0f };
1.0f, 0.0f, -1.0f
};
float sharpenkernel[] = {
0.0f, -1.0f, 0.0f,
-1.0f, 5.0f, -1.0f,
0.0f, -1.0f, 0.0f };
0.0f, -1.0f, 0.0f
};
NormalizeKernel(gaussiankernel, 9);
NormalizeKernel(sharpenkernel, 9);
@@ -65,12 +62,12 @@ int main(void)
Image catSharpend = ImageCopy(image);
ImageKernelConvolution(&catSharpend, sharpenkernel, 9);
Image catSobel = ImageCopy(image);
ImageKernelConvolution(&catSobel, sobelkernel, 9);
Image catGaussian = ImageCopy(image);
for (int i = 0; i < 6; i++)
{
ImageKernelConvolution(&catGaussian, gaussiankernel, 9);
@@ -80,14 +77,14 @@ int main(void)
ImageCrop(&catGaussian, (Rectangle){ 0, 0, (float)200, (float)450 });
ImageCrop(&catSobel, (Rectangle){ 0, 0, (float)200, (float)450 });
ImageCrop(&catSharpend, (Rectangle){ 0, 0, (float)200, (float)450 });
// Images converted to texture, GPU memory (VRAM)
Texture2D texture = LoadTextureFromImage(image);
Texture2D catSharpendTexture = LoadTextureFromImage(catSharpend);
Texture2D catSobelTexture = LoadTextureFromImage(catSobel);
Texture2D catGaussianTexture = LoadTextureFromImage(catGaussian);
// Once images have been converted to texture and uploaded to VRAM,
// Once images have been converted to texture and uploaded to VRAM,
// they can be unloaded from RAM
UnloadImage(image);
UnloadImage(catGaussian);
@@ -132,3 +129,17 @@ int main(void)
return 0;
}
//------------------------------------------------------------------------------------
// Module functions definition
//------------------------------------------------------------------------------------
static void NormalizeKernel(float *kernel, int size)
{
float sum = 0.0f;
for (int i = 0; i < size; i++) sum += kernel[i];
if (sum != 0.0f)
{
for (int i = 0; i < size; i++) kernel[i] /= sum;
}
}