浏览代码

REVIEWED: `textures_image_kernel` #3556

Added screenshot
pull/3564/head
Ray 1年前
父节点
当前提交
a3a73b9332
共有 2 个文件被更改,包括 32 次插入29 次删除
  1. +32
    -29
      examples/textures/textures_image_kernel.c
  2. 二进制
      examples/textures/textures_image_kernel.png

+ 32
- 29
examples/textures/textures_image_kernel.c 查看文件

@ -18,19 +18,14 @@
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Program main entry point // Program main entry point
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
void normalizeKernel(float *kernel, int size){
void NormalizeKernel(float *kernel, int size)
{
float sum = 0.0f; float sum = 0.0f;
for(int i = 0; i < size; i++)
{
sum += kernel[i];
}
for (int i = 0; i < size; i++) sum += kernel[i];
if(sum != 0.0f)
if (sum != 0.0f)
{ {
for(int i = 0; i < size; i++)
{
kernel[i] /= sum;
}
for (int i = 0; i < size; i++) kernel[i] /= sum;
} }
} }
@ -38,29 +33,31 @@ int main(void)
{ {
// Initialization // Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
Image image = LoadImage("resources/cat.png"); // Loaded in CPU memory (RAM)
const int screenWidth = 800; const int screenWidth = 800;
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[] = {1.0, 2.0, 1.0,
2.0, 4.0, 2.0,
1.0, 2.0, 1.0};
float gaussiankernel[] = {
1.0f, 2.0f, 1.0f,
2.0f, 4.0f, 2.0f,
1.0f, 2.0f, 1.0f };
float sobelkernel[] = {1.0, 0.0, -1.0,
2.0, 0.0, -2.0,
1.0, 0.0, -1.0};
float sobelkernel[] = {
1.0f, 0.0f, -1.0f,
2.0f, 0.0f, -2.0f,
1.0f, 0.0f, -1.0f };
float sharpenkernel[] = {0.0, -1.0, 0.0,
-1.0, 5.0, -1.0,
0.0, -1.0, 0.0};
float sharpenkernel[] = {
0.0f, -1.0f, 0.0f,
-1.0f, 5.0f, -1.0f,
0.0f, -1.0f, 0.0f };
normalizeKernel(gaussiankernel, 9);
normalizeKernel(sharpenkernel, 9);
normalizeKernel(sobelkernel, 9);
NormalizeKernel(gaussiankernel, 9);
NormalizeKernel(sharpenkernel, 9);
NormalizeKernel(sobelkernel, 9);
Image catSharpend = ImageCopy(image); Image catSharpend = ImageCopy(image);
ImageKernelConvolution(&catSharpend, sharpenkernel, 9); ImageKernelConvolution(&catSharpend, sharpenkernel, 9);
@ -69,7 +66,8 @@ int main(void)
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);
} }
@ -78,11 +76,16 @@ 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 });
Texture2D texture = LoadTextureFromImage(image); // Image converted to texture, GPU memory (VRAM)
// Images converted to texture, GPU memory (VRAM)
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);
UnloadImage(image); // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM
// Once images have been converted to texture and uploaded to VRAM,
// they can be unloaded from RAM
UnloadImage(image);
UnloadImage(catGaussian); UnloadImage(catGaussian);
UnloadImage(catSobel); UnloadImage(catSobel);
UnloadImage(catSharpend); UnloadImage(catSharpend);
@ -115,7 +118,7 @@ int main(void)
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
UnloadTexture(texture); // Texture unloading
UnloadTexture(texture);
UnloadTexture(catGaussianTexture); UnloadTexture(catGaussianTexture);
UnloadTexture(catSobelTexture); UnloadTexture(catSobelTexture);
UnloadTexture(catSharpendTexture); UnloadTexture(catSharpendTexture);

二进制
examples/textures/textures_image_kernel.png 查看文件

之前 之后
宽度: 800  |  高度: 450  |  大小: 1.0 MiB

正在加载...
取消
保存