Skip to content

Commit 9f07cfe

Browse files
committed
Update textures_image_kernel.c
1 parent f02c7fc commit 9f07cfe

File tree

1 file changed

+34
-23
lines changed

1 file changed

+34
-23
lines changed

examples/textures/textures_image_kernel.c

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*******************************************************************************************
22
*
3-
* raylib [textures] example - Image loading and texture creation
3+
* raylib [textures] example - image kernel convolution
44
*
55
* Example complexity rating: [★★★★] 4/4
66
*
@@ -20,19 +20,13 @@
2020
#include "raylib.h"
2121

2222
//------------------------------------------------------------------------------------
23-
// Program main entry point
23+
// Module functions declaration
2424
//------------------------------------------------------------------------------------
25-
void NormalizeKernel(float *kernel, int size)
26-
{
27-
float sum = 0.0f;
28-
for (int i = 0; i < size; i++) sum += kernel[i];
29-
30-
if (sum != 0.0f)
31-
{
32-
for (int i = 0; i < size; i++) kernel[i] /= sum;
33-
}
34-
}
25+
void NormalizeKernel(float *kernel, int size);
3526

27+
//------------------------------------------------------------------------------------
28+
// Program main entry point
29+
//------------------------------------------------------------------------------------
3630
int main(void)
3731
{
3832
// Initialization
@@ -41,36 +35,39 @@ int main(void)
4135
const int screenHeight = 450;
4236

4337
InitWindow(screenWidth, screenHeight, "raylib [textures] example - image convolution");
44-
45-
Image image = LoadImage("resources/cat.png"); // Loaded in CPU memory (RAM)
4638

47-
float gaussiankernel[] = {
39+
Image image = LoadImage("resources/cat.png"); // Loaded in CPU memory (RAM)
40+
41+
float gaussiankernel[] = {
4842
1.0f, 2.0f, 1.0f,
4943
2.0f, 4.0f, 2.0f,
50-
1.0f, 2.0f, 1.0f };
44+
1.0f, 2.0f, 1.0f
45+
};
5146

5247
float sobelkernel[] = {
5348
1.0f, 0.0f, -1.0f,
5449
2.0f, 0.0f, -2.0f,
55-
1.0f, 0.0f, -1.0f };
50+
1.0f, 0.0f, -1.0f
51+
};
5652

5753
float sharpenkernel[] = {
5854
0.0f, -1.0f, 0.0f,
5955
-1.0f, 5.0f, -1.0f,
60-
0.0f, -1.0f, 0.0f };
56+
0.0f, -1.0f, 0.0f
57+
};
6158

6259
NormalizeKernel(gaussiankernel, 9);
6360
NormalizeKernel(sharpenkernel, 9);
6461
NormalizeKernel(sobelkernel, 9);
6562

6663
Image catSharpend = ImageCopy(image);
6764
ImageKernelConvolution(&catSharpend, sharpenkernel, 9);
68-
65+
6966
Image catSobel = ImageCopy(image);
7067
ImageKernelConvolution(&catSobel, sobelkernel, 9);
7168

7269
Image catGaussian = ImageCopy(image);
73-
70+
7471
for (int i = 0; i < 6; i++)
7572
{
7673
ImageKernelConvolution(&catGaussian, gaussiankernel, 9);
@@ -80,14 +77,14 @@ int main(void)
8077
ImageCrop(&catGaussian, (Rectangle){ 0, 0, (float)200, (float)450 });
8178
ImageCrop(&catSobel, (Rectangle){ 0, 0, (float)200, (float)450 });
8279
ImageCrop(&catSharpend, (Rectangle){ 0, 0, (float)200, (float)450 });
83-
80+
8481
// Images converted to texture, GPU memory (VRAM)
8582
Texture2D texture = LoadTextureFromImage(image);
8683
Texture2D catSharpendTexture = LoadTextureFromImage(catSharpend);
8784
Texture2D catSobelTexture = LoadTextureFromImage(catSobel);
8885
Texture2D catGaussianTexture = LoadTextureFromImage(catGaussian);
89-
90-
// Once images have been converted to texture and uploaded to VRAM,
86+
87+
// Once images have been converted to texture and uploaded to VRAM,
9188
// they can be unloaded from RAM
9289
UnloadImage(image);
9390
UnloadImage(catGaussian);
@@ -132,3 +129,17 @@ int main(void)
132129

133130
return 0;
134131
}
132+
133+
//------------------------------------------------------------------------------------
134+
// Module functions definition
135+
//------------------------------------------------------------------------------------
136+
static void NormalizeKernel(float *kernel, int size)
137+
{
138+
float sum = 0.0f;
139+
for (int i = 0; i < size; i++) sum += kernel[i];
140+
141+
if (sum != 0.0f)
142+
{
143+
for (int i = 0; i < size; i++) kernel[i] /= sum;
144+
}
145+
}

0 commit comments

Comments
 (0)