mirror of
https://github.com/raysan5/raylib.git
synced 2025-09-06 03:18:14 +00:00
Update textures_image_kernel.c
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
/*******************************************************************************************
|
/*******************************************************************************************
|
||||||
*
|
*
|
||||||
* raylib [textures] example - Image loading and texture creation
|
* raylib [textures] example - image kernel convolution
|
||||||
*
|
*
|
||||||
* Example complexity rating: [★★★★] 4/4
|
* Example complexity rating: [★★★★] 4/4
|
||||||
*
|
*
|
||||||
@@ -19,20 +19,14 @@
|
|||||||
|
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
|
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
// Module functions declaration
|
||||||
|
//------------------------------------------------------------------------------------
|
||||||
|
void NormalizeKernel(float *kernel, int size);
|
||||||
|
|
||||||
//------------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------------
|
||||||
// Program main entry point
|
// 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)
|
int main(void)
|
||||||
{
|
{
|
||||||
// Initialization
|
// Initialization
|
||||||
@@ -41,23 +35,26 @@ int main(void)
|
|||||||
const int screenHeight = 450;
|
const int screenHeight = 450;
|
||||||
|
|
||||||
InitWindow(screenWidth, screenHeight, "raylib [textures] example - image convolution");
|
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,
|
1.0f, 2.0f, 1.0f,
|
||||||
2.0f, 4.0f, 2.0f,
|
2.0f, 4.0f, 2.0f,
|
||||||
1.0f, 2.0f, 1.0f };
|
1.0f, 2.0f, 1.0f
|
||||||
|
};
|
||||||
|
|
||||||
float sobelkernel[] = {
|
float sobelkernel[] = {
|
||||||
1.0f, 0.0f, -1.0f,
|
1.0f, 0.0f, -1.0f,
|
||||||
2.0f, 0.0f, -2.0f,
|
2.0f, 0.0f, -2.0f,
|
||||||
1.0f, 0.0f, -1.0f };
|
1.0f, 0.0f, -1.0f
|
||||||
|
};
|
||||||
|
|
||||||
float sharpenkernel[] = {
|
float sharpenkernel[] = {
|
||||||
0.0f, -1.0f, 0.0f,
|
0.0f, -1.0f, 0.0f,
|
||||||
-1.0f, 5.0f, -1.0f,
|
-1.0f, 5.0f, -1.0f,
|
||||||
0.0f, -1.0f, 0.0f };
|
0.0f, -1.0f, 0.0f
|
||||||
|
};
|
||||||
|
|
||||||
NormalizeKernel(gaussiankernel, 9);
|
NormalizeKernel(gaussiankernel, 9);
|
||||||
NormalizeKernel(sharpenkernel, 9);
|
NormalizeKernel(sharpenkernel, 9);
|
||||||
@@ -65,12 +62,12 @@ int main(void)
|
|||||||
|
|
||||||
Image catSharpend = ImageCopy(image);
|
Image catSharpend = ImageCopy(image);
|
||||||
ImageKernelConvolution(&catSharpend, sharpenkernel, 9);
|
ImageKernelConvolution(&catSharpend, sharpenkernel, 9);
|
||||||
|
|
||||||
Image catSobel = ImageCopy(image);
|
Image catSobel = ImageCopy(image);
|
||||||
ImageKernelConvolution(&catSobel, sobelkernel, 9);
|
ImageKernelConvolution(&catSobel, sobelkernel, 9);
|
||||||
|
|
||||||
Image catGaussian = ImageCopy(image);
|
Image catGaussian = ImageCopy(image);
|
||||||
|
|
||||||
for (int i = 0; i < 6; i++)
|
for (int i = 0; i < 6; i++)
|
||||||
{
|
{
|
||||||
ImageKernelConvolution(&catGaussian, gaussiankernel, 9);
|
ImageKernelConvolution(&catGaussian, gaussiankernel, 9);
|
||||||
@@ -80,14 +77,14 @@ int main(void)
|
|||||||
ImageCrop(&catGaussian, (Rectangle){ 0, 0, (float)200, (float)450 });
|
ImageCrop(&catGaussian, (Rectangle){ 0, 0, (float)200, (float)450 });
|
||||||
ImageCrop(&catSobel, (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 });
|
ImageCrop(&catSharpend, (Rectangle){ 0, 0, (float)200, (float)450 });
|
||||||
|
|
||||||
// Images converted to texture, GPU memory (VRAM)
|
// Images converted to texture, GPU memory (VRAM)
|
||||||
Texture2D texture = LoadTextureFromImage(image);
|
Texture2D texture = LoadTextureFromImage(image);
|
||||||
Texture2D catSharpendTexture = LoadTextureFromImage(catSharpend);
|
Texture2D catSharpendTexture = LoadTextureFromImage(catSharpend);
|
||||||
Texture2D catSobelTexture = LoadTextureFromImage(catSobel);
|
Texture2D catSobelTexture = LoadTextureFromImage(catSobel);
|
||||||
Texture2D catGaussianTexture = LoadTextureFromImage(catGaussian);
|
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
|
// they can be unloaded from RAM
|
||||||
UnloadImage(image);
|
UnloadImage(image);
|
||||||
UnloadImage(catGaussian);
|
UnloadImage(catGaussian);
|
||||||
@@ -132,3 +129,17 @@ int main(void)
|
|||||||
|
|
||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user