1
1
/*******************************************************************************************
2
2
*
3
- * raylib [textures] example - Image loading and texture creation
3
+ * raylib [textures] example - image kernel convolution
4
4
*
5
5
* Example complexity rating: [★★★★] 4/4
6
6
*
20
20
#include "raylib.h"
21
21
22
22
//------------------------------------------------------------------------------------
23
- // Program main entry point
23
+ // Module functions declaration
24
24
//------------------------------------------------------------------------------------
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 );
35
26
27
+ //------------------------------------------------------------------------------------
28
+ // Program main entry point
29
+ //------------------------------------------------------------------------------------
36
30
int main (void )
37
31
{
38
32
// Initialization
@@ -41,36 +35,39 @@ int main(void)
41
35
const int screenHeight = 450 ;
42
36
43
37
InitWindow (screenWidth , screenHeight , "raylib [textures] example - image convolution" );
44
-
45
- Image image = LoadImage ("resources/cat.png" ); // Loaded in CPU memory (RAM)
46
38
47
- float gaussiankernel [] = {
39
+ Image image = LoadImage ("resources/cat.png" ); // Loaded in CPU memory (RAM)
40
+
41
+ float gaussiankernel [] = {
48
42
1.0f , 2.0f , 1.0f ,
49
43
2.0f , 4.0f , 2.0f ,
50
- 1.0f , 2.0f , 1.0f };
44
+ 1.0f , 2.0f , 1.0f
45
+ };
51
46
52
47
float sobelkernel [] = {
53
48
1.0f , 0.0f , -1.0f ,
54
49
2.0f , 0.0f , -2.0f ,
55
- 1.0f , 0.0f , -1.0f };
50
+ 1.0f , 0.0f , -1.0f
51
+ };
56
52
57
53
float sharpenkernel [] = {
58
54
0.0f , -1.0f , 0.0f ,
59
55
-1.0f , 5.0f , -1.0f ,
60
- 0.0f , -1.0f , 0.0f };
56
+ 0.0f , -1.0f , 0.0f
57
+ };
61
58
62
59
NormalizeKernel (gaussiankernel , 9 );
63
60
NormalizeKernel (sharpenkernel , 9 );
64
61
NormalizeKernel (sobelkernel , 9 );
65
62
66
63
Image catSharpend = ImageCopy (image );
67
64
ImageKernelConvolution (& catSharpend , sharpenkernel , 9 );
68
-
65
+
69
66
Image catSobel = ImageCopy (image );
70
67
ImageKernelConvolution (& catSobel , sobelkernel , 9 );
71
68
72
69
Image catGaussian = ImageCopy (image );
73
-
70
+
74
71
for (int i = 0 ; i < 6 ; i ++ )
75
72
{
76
73
ImageKernelConvolution (& catGaussian , gaussiankernel , 9 );
@@ -80,14 +77,14 @@ int main(void)
80
77
ImageCrop (& catGaussian , (Rectangle ){ 0 , 0 , (float )200 , (float )450 });
81
78
ImageCrop (& catSobel , (Rectangle ){ 0 , 0 , (float )200 , (float )450 });
82
79
ImageCrop (& catSharpend , (Rectangle ){ 0 , 0 , (float )200 , (float )450 });
83
-
80
+
84
81
// Images converted to texture, GPU memory (VRAM)
85
82
Texture2D texture = LoadTextureFromImage (image );
86
83
Texture2D catSharpendTexture = LoadTextureFromImage (catSharpend );
87
84
Texture2D catSobelTexture = LoadTextureFromImage (catSobel );
88
85
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,
91
88
// they can be unloaded from RAM
92
89
UnloadImage (image );
93
90
UnloadImage (catGaussian );
@@ -132,3 +129,17 @@ int main(void)
132
129
133
130
return 0 ;
134
131
}
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