From 590f714c26fc8d7ee86451b7d7c4f351f7e0a987 Mon Sep 17 00:00:00 2001 From: kimo-s Date: Sat, 11 Nov 2023 17:24:10 -0500 Subject: [PATCH 1/5] Added image convultion ImageKernelConvolution --- examples/Makefile | 1 + examples/textures/textures_image_kernel.c | 104 +++++++++++++++ .../raylib_npp_parser/raylib_to_parse.h | 1 + src/raylib.h | 1 + src/rtextures.c | 126 ++++++++++++++++++ 5 files changed, 233 insertions(+) create mode 100644 examples/textures/textures_image_kernel.c diff --git a/examples/Makefile b/examples/Makefile index 5cd8e6bba62a..11669e3a3a0b 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -490,6 +490,7 @@ TEXTURES = \ textures/textures_gif_player \ textures/textures_image_drawing \ textures/textures_image_generation \ + textures/textures_image_kernel \ textures/textures_image_loading \ textures/textures_image_processing \ textures/textures_image_rotate \ diff --git a/examples/textures/textures_image_kernel.c b/examples/textures/textures_image_kernel.c new file mode 100644 index 000000000000..ba120f108958 --- /dev/null +++ b/examples/textures/textures_image_kernel.c @@ -0,0 +1,104 @@ +/******************************************************************************************* +* +* raylib [textures] example - Image loading and texture creation +* +* NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM) +* +* Example originally created with raylib 1.3, last time updated with raylib 1.3 +* +* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, +* BSD-like license that allows static linking with closed source software +* +* Copyright (c) 2015-2023 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + + Image image = LoadImage("resources/cat.png"); // Loaded in CPU memory (RAM) + + const int screenWidth = image.width*4; + const int screenHeight = image.height; + + InitWindow(screenWidth, screenHeight, "raylib [textures] example - image convolution"); + + float gaussiankernel[] = {1.0, 2.0, 1.0, + 2.0, 4.0, 2.0, + 1.0, 2.0, 1.0}; + + float sobelkernel[] = {1.0, 0.0, -1.0, + 2.0, 0.0, -2.0, + 1.0, 0.0, -1.0}; + + float sharpenkernel[] = {0.0, 1.0, 0.0, + -1.0, 5.0, -1.0, + 0.0, -1.0, 0.0}; + + Image catSharpend = ImageCopy(image); + ImageKernelConvolution(&catSharpend, sharpenkernel, 3); + + + Image catSobel = ImageCopy(image); + ImageKernelConvolution(&catSobel, sobelkernel, 3); + + Image catGaussian = ImageCopy(image); + for(int i = 0; i < 6; i++){ + ImageKernelConvolution(&catGaussian, gaussiankernel, 3); + } + + + Texture2D texture = LoadTextureFromImage(image); // Image converted to texture, GPU memory (VRAM) + Texture2D catSharpendTexture = LoadTextureFromImage(catSharpend); + Texture2D catSobelTexture = LoadTextureFromImage(catSobel); + Texture2D catGaussianTexture = LoadTextureFromImage(catGaussian); + UnloadImage(image); // Once image has been converted to texture and uploaded to VRAM, it can be unloaded from RAM + UnloadImage(catGaussian); + UnloadImage(catSobel); + UnloadImage(catSharpend); + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //--------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + // TODO: Update your variables here + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + DrawTexture(catSharpendTexture, 0, 0, WHITE); + DrawTexture(catSobelTexture, texture.width, 0, WHITE); + DrawTexture(catGaussianTexture, texture.width*2, 0, WHITE); + DrawTexture(texture, texture.width*3, 0, WHITE); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadTexture(texture); // Texture unloading + UnloadTexture(catGaussianTexture); + UnloadTexture(catSobelTexture); + UnloadTexture(catSharpendTexture); + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} diff --git a/projects/Notepad++/raylib_npp_parser/raylib_to_parse.h b/projects/Notepad++/raylib_npp_parser/raylib_to_parse.h index c32cdb26ef07..68b9cad76dd8 100644 --- a/projects/Notepad++/raylib_npp_parser/raylib_to_parse.h +++ b/projects/Notepad++/raylib_npp_parser/raylib_to_parse.h @@ -339,6 +339,7 @@ RLAPI void ImageAlphaClear(Image *image, Color color, float threshold); RLAPI void ImageAlphaMask(Image *image, Image alphaMask); // Apply alpha mask to image RLAPI void ImageAlphaPremultiply(Image *image); // Premultiply alpha channel RLAPI void ImageBlurGaussian(Image *image, int blurSize); // Apply Gaussian blur using a box blur approximation +RLAPI void ImageKernelConvolution(Image *image, float* karnel, int karenlWidth); // Apply Gaussian blur using a box blur approximation RLAPI void ImageResize(Image *image, int newWidth, int newHeight); // Resize image (Bicubic scaling algorithm) RLAPI void ImageResizeNN(Image *image, int newWidth,int newHeight); // Resize image (Nearest-Neighbor scaling algorithm) RLAPI void ImageResizeCanvas(Image *image, int newWidth, int newHeight, int offsetX, int offsetY, Color fill); // Resize canvas and fill with color diff --git a/src/raylib.h b/src/raylib.h index 1c4c4a09fa23..f211c3c35474 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1329,6 +1329,7 @@ RLAPI void ImageAlphaClear(Image *image, Color color, float threshold); RLAPI void ImageAlphaMask(Image *image, Image alphaMask); // Apply alpha mask to image RLAPI void ImageAlphaPremultiply(Image *image); // Premultiply alpha channel RLAPI void ImageBlurGaussian(Image *image, int blurSize); // Apply Gaussian blur using a box blur approximation +RLAPI void ImageKernelConvolution(Image *image, float* karnel, int karenlWidth); // Apply Gaussian blur using a box blur approximation RLAPI void ImageResize(Image *image, int newWidth, int newHeight); // Resize image (Bicubic scaling algorithm) RLAPI void ImageResizeNN(Image *image, int newWidth,int newHeight); // Resize image (Nearest-Neighbor scaling algorithm) RLAPI void ImageResizeCanvas(Image *image, int newWidth, int newHeight, int offsetX, int offsetY, Color fill); // Resize canvas and fill with color diff --git a/src/rtextures.c b/src/rtextures.c index 98586db73b2f..0572692a2051 100644 --- a/src/rtextures.c +++ b/src/rtextures.c @@ -2082,6 +2082,132 @@ void ImageBlurGaussian(Image *image, int blurSize) { ImageFormat(image, format); } +// The kernel matrix is assumed to be square. Only supply the width of the kernel. +void ImageKernelConvolution(Image *image, float* karnel, int karenlWidth){ + + if ((image->data == NULL) || (image->width == 0) || (image->height == 0) || karnel == NULL) return; + + ImageAlphaPremultiply(image); + + Color *pixels = LoadImageColors(*image); + + Vector4 *imageCopy1 = RL_MALLOC((image->height)*(image->width)*sizeof(Vector4)); + Vector4 *imageCopy2 = RL_MALLOC((image->height)*(image->width)*sizeof(Vector4)); + Vector4 *temp = RL_MALLOC(karenlWidth*karenlWidth*sizeof(Vector4)); + + + float normKernel = 0.0f; + for(int i = 0; i < karenlWidth * karenlWidth; i++){ + temp[i].x = 0.0f; + temp[i].y = 0.0f; + temp[i].z = 0.0f; + temp[i].w = 0.0f; + normKernel += karnel[i]; + } + + if(normKernel != 0.0f){ + for(int i = 0; i < karenlWidth * karenlWidth; i++){ + karnel[i] /= normKernel; + } + } + + + float rRes = 0.0f; + float gRes = 0.0f; + float bRes = 0.0f; + float aRes = 0.0f; + + for (int i = 0; i < (image->height)*(image->width); i++) { + imageCopy1[i].x = ((float)pixels[i].r)/255.0f; + imageCopy1[i].y = ((float)pixels[i].g)/255.0f; + imageCopy1[i].z = ((float)pixels[i].b)/255.0f; + imageCopy1[i].w = ((float)pixels[i].a)/255.0f; + } + + + + int startRange, endRange; + if(karenlWidth % 2 == 0){ + startRange = -karenlWidth/2; + endRange = karenlWidth/2; + } else { + startRange = -karenlWidth/2; + endRange = karenlWidth/2+1; + } + for(int x = 0; x < image->height; x++) { + for(int y = 0; y < image->width; y++) { + + for(int xk = startRange; xk < endRange; xk++){ + for(int yk = startRange; yk < endRange; yk++){ + int xkabs = xk + karenlWidth/2; + int ykabs = yk + karenlWidth/2; + size_t imgindex = image->width * (x+xk) + (y+yk); + if(imgindex < 0 || imgindex >= image->width * image->height){ + temp[karenlWidth * xkabs + ykabs].x = 0.0f; + temp[karenlWidth * xkabs + ykabs].y = 0.0f; + temp[karenlWidth * xkabs + ykabs].z = 0.0f; + temp[karenlWidth * xkabs + ykabs].w = 0.0f; + } else { + temp[karenlWidth * xkabs + ykabs].x = imageCopy1[imgindex].x * karnel[karenlWidth * xkabs + ykabs]; + temp[karenlWidth * xkabs + ykabs].y = imageCopy1[imgindex].y * karnel[karenlWidth * xkabs + ykabs]; + temp[karenlWidth * xkabs + ykabs].z = imageCopy1[imgindex].z * karnel[karenlWidth * xkabs + ykabs]; + temp[karenlWidth * xkabs + ykabs].w = imageCopy1[imgindex].w * karnel[karenlWidth * xkabs + ykabs]; + } + } + } + + for(int i = 0; i < karenlWidth * karenlWidth; i++){ + rRes += temp[i].x; + gRes += temp[i].y; + bRes += temp[i].z; + aRes += temp[i].w; + } + + STBIR_CLAMP(rRes, 0.0f, 1.0f); + STBIR_CLAMP(gRes, 0.0f, 1.0f); + STBIR_CLAMP(bRes, 0.0f, 1.0f); + STBIR_CLAMP(aRes, 0.0f, 1.0f); + + imageCopy2[image->width * (x) + (y)].x = rRes; + imageCopy2[image->width * (x) + (y)].y = gRes; + imageCopy2[image->width * (x) + (y)].z = bRes; + imageCopy2[image->width * (x) + (y)].w = aRes; + + rRes = 0.0f; + gRes = 0.0f; + bRes = 0.0f; + aRes = 0.0f; + + for(int i = 0; i < karenlWidth * karenlWidth; i++){ + temp[i].x = 0.0f; + temp[i].y = 0.0f; + temp[i].z = 0.0f; + temp[i].w = 0.0f; + } + } + } + + for (int i = 0; i < (image->width)*(image->height); i++) { + float alpha = (float)imageCopy2[i].w; + pixels[i].r = (unsigned char)((imageCopy2[i].x)*255.0f); + pixels[i].g = (unsigned char)((imageCopy2[i].y)*255.0f); + pixels[i].b = (unsigned char)((imageCopy2[i].z)*255.0f); + pixels[i].a = (unsigned char)((alpha)*255.0f); + // printf("pixels[%d] = %d", i, pixels[i].r); + } + + + int format = image->format; + RL_FREE(image->data); + RL_FREE(imageCopy1); + RL_FREE(imageCopy2); + RL_FREE(temp); + + image->data = pixels; + image->format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8; + ImageFormat(image, format); +} + // Generate all mipmap levels for a provided image // NOTE 1: Supports POT and NPOT images // NOTE 2: image.data is scaled to include mipmap levels From b6d07d2a291220dabaf3f07d7540a5fe1b9dc739 Mon Sep 17 00:00:00 2001 From: kimo-s Date: Sat, 11 Nov 2023 17:38:21 -0500 Subject: [PATCH 2/5] comment changes --- projects/Notepad++/raylib_npp_parser/raylib_to_parse.h | 2 +- src/raylib.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/projects/Notepad++/raylib_npp_parser/raylib_to_parse.h b/projects/Notepad++/raylib_npp_parser/raylib_to_parse.h index 68b9cad76dd8..364265ef3772 100644 --- a/projects/Notepad++/raylib_npp_parser/raylib_to_parse.h +++ b/projects/Notepad++/raylib_npp_parser/raylib_to_parse.h @@ -339,7 +339,7 @@ RLAPI void ImageAlphaClear(Image *image, Color color, float threshold); RLAPI void ImageAlphaMask(Image *image, Image alphaMask); // Apply alpha mask to image RLAPI void ImageAlphaPremultiply(Image *image); // Premultiply alpha channel RLAPI void ImageBlurGaussian(Image *image, int blurSize); // Apply Gaussian blur using a box blur approximation -RLAPI void ImageKernelConvolution(Image *image, float* karnel, int karenlWidth); // Apply Gaussian blur using a box blur approximation +RLAPI void ImageKernelConvolution(Image *image, float* karnel, int karenlWidth); // Apply Custom Square image convolution kernel RLAPI void ImageResize(Image *image, int newWidth, int newHeight); // Resize image (Bicubic scaling algorithm) RLAPI void ImageResizeNN(Image *image, int newWidth,int newHeight); // Resize image (Nearest-Neighbor scaling algorithm) RLAPI void ImageResizeCanvas(Image *image, int newWidth, int newHeight, int offsetX, int offsetY, Color fill); // Resize canvas and fill with color diff --git a/src/raylib.h b/src/raylib.h index f211c3c35474..4c1755715455 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1329,7 +1329,7 @@ RLAPI void ImageAlphaClear(Image *image, Color color, float threshold); RLAPI void ImageAlphaMask(Image *image, Image alphaMask); // Apply alpha mask to image RLAPI void ImageAlphaPremultiply(Image *image); // Premultiply alpha channel RLAPI void ImageBlurGaussian(Image *image, int blurSize); // Apply Gaussian blur using a box blur approximation -RLAPI void ImageKernelConvolution(Image *image, float* karnel, int karenlWidth); // Apply Gaussian blur using a box blur approximation +RLAPI void ImageKernelConvolution(Image *image, float* karnel, int karenlWidth); // Apply Custom Square image convolution kernel RLAPI void ImageResize(Image *image, int newWidth, int newHeight); // Resize image (Bicubic scaling algorithm) RLAPI void ImageResizeNN(Image *image, int newWidth,int newHeight); // Resize image (Nearest-Neighbor scaling algorithm) RLAPI void ImageResizeCanvas(Image *image, int newWidth, int newHeight, int offsetX, int offsetY, Color fill); // Resize canvas and fill with color From 6d286a7837a2038804cc68b32df19402ec011497 Mon Sep 17 00:00:00 2001 From: kimo-s Date: Wed, 15 Nov 2023 13:59:51 -0500 Subject: [PATCH 3/5] spelling changes and change to kernel size --- examples/textures/textures_image_kernel.c | 24 ++--- .../raylib_npp_parser/raylib_to_parse.h | 2 +- src/raylib.h | 2 +- src/rtextures.c | 89 ++++++++++--------- 4 files changed, 64 insertions(+), 53 deletions(-) diff --git a/examples/textures/textures_image_kernel.c b/examples/textures/textures_image_kernel.c index ba120f108958..781e47d8c779 100644 --- a/examples/textures/textures_image_kernel.c +++ b/examples/textures/textures_image_kernel.c @@ -9,7 +9,7 @@ * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, * BSD-like license that allows static linking with closed source software * -* Copyright (c) 2015-2023 Ramon Santamaria (@raysan5) +* Copyright (c) 2015-2023 Karim Salem (@kimo-s) * ********************************************************************************************/ @@ -25,8 +25,8 @@ int main(void) Image image = LoadImage("resources/cat.png"); // Loaded in CPU memory (RAM) - const int screenWidth = image.width*4; - const int screenHeight = image.height; + const int screenWidth = 800; + const int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [textures] example - image convolution"); @@ -43,18 +43,20 @@ int main(void) 0.0, -1.0, 0.0}; Image catSharpend = ImageCopy(image); - ImageKernelConvolution(&catSharpend, sharpenkernel, 3); + ImageKernelConvolution(&catSharpend, sharpenkernel, 16); - Image catSobel = ImageCopy(image); - ImageKernelConvolution(&catSobel, sobelkernel, 3); + ImageKernelConvolution(&catSobel, sobelkernel, 16); Image catGaussian = ImageCopy(image); for(int i = 0; i < 6; i++){ - ImageKernelConvolution(&catGaussian, gaussiankernel, 3); + ImageKernelConvolution(&catGaussian, gaussiankernel, 16); } - + ImageCrop(&image, (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(&catSharpend, (Rectangle){ 0, 0, (float)200, (float)450 }); Texture2D texture = LoadTextureFromImage(image); // Image converted to texture, GPU memory (VRAM) Texture2D catSharpendTexture = LoadTextureFromImage(catSharpend); Texture2D catSobelTexture = LoadTextureFromImage(catSobel); @@ -82,9 +84,9 @@ int main(void) ClearBackground(RAYWHITE); DrawTexture(catSharpendTexture, 0, 0, WHITE); - DrawTexture(catSobelTexture, texture.width, 0, WHITE); - DrawTexture(catGaussianTexture, texture.width*2, 0, WHITE); - DrawTexture(texture, texture.width*3, 0, WHITE); + DrawTexture(catSobelTexture, 200, 0, WHITE); + DrawTexture(catGaussianTexture, 400, 0, WHITE); + DrawTexture(texture, 600, 0, WHITE); EndDrawing(); //---------------------------------------------------------------------------------- diff --git a/projects/Notepad++/raylib_npp_parser/raylib_to_parse.h b/projects/Notepad++/raylib_npp_parser/raylib_to_parse.h index 364265ef3772..58dc94f54002 100644 --- a/projects/Notepad++/raylib_npp_parser/raylib_to_parse.h +++ b/projects/Notepad++/raylib_npp_parser/raylib_to_parse.h @@ -339,7 +339,7 @@ RLAPI void ImageAlphaClear(Image *image, Color color, float threshold); RLAPI void ImageAlphaMask(Image *image, Image alphaMask); // Apply alpha mask to image RLAPI void ImageAlphaPremultiply(Image *image); // Premultiply alpha channel RLAPI void ImageBlurGaussian(Image *image, int blurSize); // Apply Gaussian blur using a box blur approximation -RLAPI void ImageKernelConvolution(Image *image, float* karnel, int karenlWidth); // Apply Custom Square image convolution kernel +RLAPI void ImageKernelConvolution(Image *image, float* kernel, int kernelSize); // Apply Custom Square image convolution kernel RLAPI void ImageResize(Image *image, int newWidth, int newHeight); // Resize image (Bicubic scaling algorithm) RLAPI void ImageResizeNN(Image *image, int newWidth,int newHeight); // Resize image (Nearest-Neighbor scaling algorithm) RLAPI void ImageResizeCanvas(Image *image, int newWidth, int newHeight, int offsetX, int offsetY, Color fill); // Resize canvas and fill with color diff --git a/src/raylib.h b/src/raylib.h index 4c1755715455..43ee5f8fe0ef 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1329,7 +1329,7 @@ RLAPI void ImageAlphaClear(Image *image, Color color, float threshold); RLAPI void ImageAlphaMask(Image *image, Image alphaMask); // Apply alpha mask to image RLAPI void ImageAlphaPremultiply(Image *image); // Premultiply alpha channel RLAPI void ImageBlurGaussian(Image *image, int blurSize); // Apply Gaussian blur using a box blur approximation -RLAPI void ImageKernelConvolution(Image *image, float* karnel, int karenlWidth); // Apply Custom Square image convolution kernel +RLAPI void ImageKernelConvolution(Image *image, float* kernel, int kernelSize); // Apply Custom Square image convolution kernel RLAPI void ImageResize(Image *image, int newWidth, int newHeight); // Resize image (Bicubic scaling algorithm) RLAPI void ImageResizeNN(Image *image, int newWidth,int newHeight); // Resize image (Nearest-Neighbor scaling algorithm) RLAPI void ImageResizeCanvas(Image *image, int newWidth, int newHeight, int offsetX, int offsetY, Color fill); // Resize canvas and fill with color diff --git a/src/rtextures.c b/src/rtextures.c index 0572692a2051..7cc38d14a86a 100644 --- a/src/rtextures.c +++ b/src/rtextures.c @@ -2083,90 +2083,100 @@ void ImageBlurGaussian(Image *image, int blurSize) { } // The kernel matrix is assumed to be square. Only supply the width of the kernel. -void ImageKernelConvolution(Image *image, float* karnel, int karenlWidth){ +void ImageKernelConvolution(Image *image, float* kernel, int kernelSize){ - if ((image->data == NULL) || (image->width == 0) || (image->height == 0) || karnel == NULL) return; + if ((image->data == NULL) || (image->width == 0) || (image->height == 0) || kernel == NULL) return; - ImageAlphaPremultiply(image); + int kernelWidth = (int)sqrtf((float)kernelSize); + if (kernelWidth*kernelWidth != kernelSize) + { + TRACELOG(LOG_WARNING, "IMAGE: Convolution kernel must be square to be applied"); + return; + } Color *pixels = LoadImageColors(*image); - Vector4 *imageCopy1 = RL_MALLOC((image->height)*(image->width)*sizeof(Vector4)); Vector4 *imageCopy2 = RL_MALLOC((image->height)*(image->width)*sizeof(Vector4)); - Vector4 *temp = RL_MALLOC(karenlWidth*karenlWidth*sizeof(Vector4)); + Vector4 *temp = RL_MALLOC(kernelSize*sizeof(Vector4)); float normKernel = 0.0f; - for(int i = 0; i < karenlWidth * karenlWidth; i++){ + for(int i = 0; i < kernelSize; i++){ temp[i].x = 0.0f; temp[i].y = 0.0f; temp[i].z = 0.0f; temp[i].w = 0.0f; - normKernel += karnel[i]; + normKernel += kernel[i]; } if(normKernel != 0.0f){ - for(int i = 0; i < karenlWidth * karenlWidth; i++){ - karnel[i] /= normKernel; + for(int i = 0; i < kernelSize; i++){ + kernel[i] /= normKernel; } } - float rRes = 0.0f; float gRes = 0.0f; float bRes = 0.0f; float aRes = 0.0f; - for (int i = 0; i < (image->height)*(image->width); i++) { - imageCopy1[i].x = ((float)pixels[i].r)/255.0f; - imageCopy1[i].y = ((float)pixels[i].g)/255.0f; - imageCopy1[i].z = ((float)pixels[i].b)/255.0f; - imageCopy1[i].w = ((float)pixels[i].a)/255.0f; - } - - int startRange, endRange; - if(karenlWidth % 2 == 0){ - startRange = -karenlWidth/2; - endRange = karenlWidth/2; + if(kernelWidth % 2 == 0){ + startRange = -kernelWidth/2; + endRange = kernelWidth/2; } else { - startRange = -karenlWidth/2; - endRange = karenlWidth/2+1; + startRange = -kernelWidth/2; + endRange = kernelWidth/2+1; } for(int x = 0; x < image->height; x++) { for(int y = 0; y < image->width; y++) { for(int xk = startRange; xk < endRange; xk++){ for(int yk = startRange; yk < endRange; yk++){ - int xkabs = xk + karenlWidth/2; - int ykabs = yk + karenlWidth/2; + int xkabs = xk + kernelWidth/2; + int ykabs = yk + kernelWidth/2; size_t imgindex = image->width * (x+xk) + (y+yk); if(imgindex < 0 || imgindex >= image->width * image->height){ - temp[karenlWidth * xkabs + ykabs].x = 0.0f; - temp[karenlWidth * xkabs + ykabs].y = 0.0f; - temp[karenlWidth * xkabs + ykabs].z = 0.0f; - temp[karenlWidth * xkabs + ykabs].w = 0.0f; + temp[kernelWidth * xkabs + ykabs].x = 0.0f; + temp[kernelWidth * xkabs + ykabs].y = 0.0f; + temp[kernelWidth * xkabs + ykabs].z = 0.0f; + temp[kernelWidth * xkabs + ykabs].w = 0.0f; } else { - temp[karenlWidth * xkabs + ykabs].x = imageCopy1[imgindex].x * karnel[karenlWidth * xkabs + ykabs]; - temp[karenlWidth * xkabs + ykabs].y = imageCopy1[imgindex].y * karnel[karenlWidth * xkabs + ykabs]; - temp[karenlWidth * xkabs + ykabs].z = imageCopy1[imgindex].z * karnel[karenlWidth * xkabs + ykabs]; - temp[karenlWidth * xkabs + ykabs].w = imageCopy1[imgindex].w * karnel[karenlWidth * xkabs + ykabs]; + temp[kernelWidth * xkabs + ykabs].x = ((float)pixels[imgindex].r)/255.0f * kernel[kernelWidth * xkabs + ykabs]; + temp[kernelWidth * xkabs + ykabs].y = ((float)pixels[imgindex].g)/255.0f * kernel[kernelWidth * xkabs + ykabs]; + temp[kernelWidth * xkabs + ykabs].z = ((float)pixels[imgindex].b)/255.0f * kernel[kernelWidth * xkabs + ykabs]; + temp[kernelWidth * xkabs + ykabs].w = ((float)pixels[imgindex].a)/255.0f * kernel[kernelWidth * xkabs + ykabs]; } } } - for(int i = 0; i < karenlWidth * karenlWidth; i++){ + for(int i = 0; i < kernelSize; i++){ rRes += temp[i].x; gRes += temp[i].y; bRes += temp[i].z; aRes += temp[i].w; } - STBIR_CLAMP(rRes, 0.0f, 1.0f); - STBIR_CLAMP(gRes, 0.0f, 1.0f); - STBIR_CLAMP(bRes, 0.0f, 1.0f); - STBIR_CLAMP(aRes, 0.0f, 1.0f); + if(rRes < 0.0f){ + rRes = 0.0f; + } + if(gRes < 0.0f){ + gRes = 0.0f; + } + if(bRes < 0.0f){ + bRes = 0.0f; + } + + if(rRes > 1.0f){ + rRes = 1.0f; + } + if(gRes > 1.0f){ + gRes = 1.0f; + } + if(bRes > 1.0f){ + bRes = 1.0f; + } imageCopy2[image->width * (x) + (y)].x = rRes; imageCopy2[image->width * (x) + (y)].y = gRes; @@ -2178,7 +2188,7 @@ void ImageKernelConvolution(Image *image, float* karnel, int karenlWidth){ bRes = 0.0f; aRes = 0.0f; - for(int i = 0; i < karenlWidth * karenlWidth; i++){ + for(int i = 0; i < kernelSize; i++){ temp[i].x = 0.0f; temp[i].y = 0.0f; temp[i].z = 0.0f; @@ -2199,7 +2209,6 @@ void ImageKernelConvolution(Image *image, float* karnel, int karenlWidth){ int format = image->format; RL_FREE(image->data); - RL_FREE(imageCopy1); RL_FREE(imageCopy2); RL_FREE(temp); From eff1b59f46b92c01a88c8f921e92ad5a28cde47c Mon Sep 17 00:00:00 2001 From: kimo-s Date: Wed, 15 Nov 2023 14:51:24 -0500 Subject: [PATCH 4/5] removed kernel normalization inside function --- examples/textures/textures_image_kernel.c | 25 +++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/examples/textures/textures_image_kernel.c b/examples/textures/textures_image_kernel.c index 781e47d8c779..5312c29d3b4d 100644 --- a/examples/textures/textures_image_kernel.c +++ b/examples/textures/textures_image_kernel.c @@ -18,6 +18,19 @@ //------------------------------------------------------------------------------------ // 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 @@ -38,19 +51,23 @@ int main(void) 2.0, 0.0, -2.0, 1.0, 0.0, -1.0}; - float sharpenkernel[] = {0.0, 1.0, 0.0, + float sharpenkernel[] = {0.0, -1.0, 0.0, -1.0, 5.0, -1.0, 0.0, -1.0, 0.0}; + normalizeKernel(gaussiankernel, 9); + normalizeKernel(sharpenkernel, 9); + normalizeKernel(sobelkernel, 9); + Image catSharpend = ImageCopy(image); - ImageKernelConvolution(&catSharpend, sharpenkernel, 16); + ImageKernelConvolution(&catSharpend, sharpenkernel, 9); Image catSobel = ImageCopy(image); - ImageKernelConvolution(&catSobel, sobelkernel, 16); + ImageKernelConvolution(&catSobel, sobelkernel, 9); Image catGaussian = ImageCopy(image); for(int i = 0; i < 6; i++){ - ImageKernelConvolution(&catGaussian, gaussiankernel, 16); + ImageKernelConvolution(&catGaussian, gaussiankernel, 9); } ImageCrop(&image, (Rectangle){ 0, 0, (float)200, (float)450 }); From 3d4df8ac7f20860797221a490683076b56aef93a Mon Sep 17 00:00:00 2001 From: kimo-s Date: Wed, 15 Nov 2023 14:56:20 -0500 Subject: [PATCH 5/5] fix to formating --- examples/textures/textures_image_kernel.c | 12 +++-- src/rtextures.c | 53 +++++++++++++---------- 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/examples/textures/textures_image_kernel.c b/examples/textures/textures_image_kernel.c index 5312c29d3b4d..cbc75e18effc 100644 --- a/examples/textures/textures_image_kernel.c +++ b/examples/textures/textures_image_kernel.c @@ -20,12 +20,15 @@ //------------------------------------------------------------------------------------ void normalizeKernel(float *kernel, int size){ float sum = 0.0f; - for(int i = 0; i < size; i++){ + for(int i = 0; i < size; i++) + { sum += kernel[i]; } - if(sum != 0.0f){ - for(int i = 0; i < size; i++){ + if(sum != 0.0f) + { + for(int i = 0; i < size; i++) + { kernel[i] /= sum; } } @@ -66,7 +69,8 @@ int main(void) ImageKernelConvolution(&catSobel, sobelkernel, 9); Image catGaussian = ImageCopy(image); - for(int i = 0; i < 6; i++){ + for(int i = 0; i < 6; i++) + { ImageKernelConvolution(&catGaussian, gaussiankernel, 9); } diff --git a/src/rtextures.c b/src/rtextures.c index 7cc38d14a86a..8742b0c9ce35 100644 --- a/src/rtextures.c +++ b/src/rtextures.c @@ -2100,19 +2100,11 @@ void ImageKernelConvolution(Image *image, float* kernel, int kernelSize){ Vector4 *temp = RL_MALLOC(kernelSize*sizeof(Vector4)); - float normKernel = 0.0f; for(int i = 0; i < kernelSize; i++){ temp[i].x = 0.0f; temp[i].y = 0.0f; temp[i].z = 0.0f; temp[i].w = 0.0f; - normKernel += kernel[i]; - } - - if(normKernel != 0.0f){ - for(int i = 0; i < kernelSize; i++){ - kernel[i] /= normKernel; - } } float rRes = 0.0f; @@ -2122,18 +2114,24 @@ void ImageKernelConvolution(Image *image, float* kernel, int kernelSize){ int startRange, endRange; - if(kernelWidth % 2 == 0){ + if(kernelWidth % 2 == 0) + { startRange = -kernelWidth/2; endRange = kernelWidth/2; - } else { + } else + { startRange = -kernelWidth/2; endRange = kernelWidth/2+1; } - for(int x = 0; x < image->height; x++) { - for(int y = 0; y < image->width; y++) { + for(int x = 0; x < image->height; x++) + { + for(int y = 0; y < image->width; y++) + { - for(int xk = startRange; xk < endRange; xk++){ - for(int yk = startRange; yk < endRange; yk++){ + for(int xk = startRange; xk < endRange; xk++) + { + for(int yk = startRange; yk < endRange; yk++) + { int xkabs = xk + kernelWidth/2; int ykabs = yk + kernelWidth/2; size_t imgindex = image->width * (x+xk) + (y+yk); @@ -2151,30 +2149,37 @@ void ImageKernelConvolution(Image *image, float* kernel, int kernelSize){ } } - for(int i = 0; i < kernelSize; i++){ + for(int i = 0; i < kernelSize; i++) + { rRes += temp[i].x; gRes += temp[i].y; bRes += temp[i].z; aRes += temp[i].w; } - if(rRes < 0.0f){ + if(rRes < 0.0f) + { rRes = 0.0f; } - if(gRes < 0.0f){ + if(gRes < 0.0f) + { gRes = 0.0f; } - if(bRes < 0.0f){ + if(bRes < 0.0f) + { bRes = 0.0f; } - if(rRes > 1.0f){ + if(rRes > 1.0f) + { rRes = 1.0f; } - if(gRes > 1.0f){ + if(gRes > 1.0f) + { gRes = 1.0f; } - if(bRes > 1.0f){ + if(bRes > 1.0f) + { bRes = 1.0f; } @@ -2188,7 +2193,8 @@ void ImageKernelConvolution(Image *image, float* kernel, int kernelSize){ bRes = 0.0f; aRes = 0.0f; - for(int i = 0; i < kernelSize; i++){ + for(int i = 0; i < kernelSize; i++) + { temp[i].x = 0.0f; temp[i].y = 0.0f; temp[i].z = 0.0f; @@ -2197,7 +2203,8 @@ void ImageKernelConvolution(Image *image, float* kernel, int kernelSize){ } } - for (int i = 0; i < (image->width)*(image->height); i++) { + for (int i = 0; i < (image->width) * (image->height); i++) + { float alpha = (float)imageCopy2[i].w; pixels[i].r = (unsigned char)((imageCopy2[i].x)*255.0f); pixels[i].g = (unsigned char)((imageCopy2[i].y)*255.0f);