diff options
| author | Karim <[email protected]> | 2023-11-18 14:05:45 -0500 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-11-18 20:05:45 +0100 |
| commit | 21469e92b0a886de8b384b7ffadc5c1c73cff913 (patch) | |
| tree | 711cb5f9f103a53cb712ce3460a4698d159d1824 /src | |
| parent | e41a0c9721a38c81a1499ae499b746bd05f9de2e (diff) | |
| download | raylib-21469e92b0a886de8b384b7ffadc5c1c73cff913.tar.gz raylib-21469e92b0a886de8b384b7ffadc5c1c73cff913.zip | |
Image convolution function ImageKernelConvolution (#3528)
* Added image convultion ImageKernelConvolution
* comment changes
* spelling changes and change to kernel size
* removed kernel normalization inside function
* fix to formating
Diffstat (limited to 'src')
| -rw-r--r-- | src/raylib.h | 1 | ||||
| -rw-r--r-- | src/rtextures.c | 142 |
2 files changed, 143 insertions, 0 deletions
diff --git a/src/raylib.h b/src/raylib.h index 6ea00300..28f052c7 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* 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 98586db7..8742b0c9 100644 --- a/src/rtextures.c +++ b/src/rtextures.c @@ -2082,6 +2082,148 @@ 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* kernel, int kernelSize){ + + if ((image->data == NULL) || (image->width == 0) || (image->height == 0) || kernel == NULL) return; + + 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 *imageCopy2 = RL_MALLOC((image->height)*(image->width)*sizeof(Vector4)); + Vector4 *temp = RL_MALLOC(kernelSize*sizeof(Vector4)); + + + 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; + } + + float rRes = 0.0f; + float gRes = 0.0f; + float bRes = 0.0f; + float aRes = 0.0f; + + + int startRange, endRange; + if(kernelWidth % 2 == 0) + { + startRange = -kernelWidth/2; + endRange = kernelWidth/2; + } 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 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); + if(imgindex < 0 || imgindex >= image->width * image->height){ + 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[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 < kernelSize; i++) + { + rRes += temp[i].x; + gRes += temp[i].y; + bRes += temp[i].z; + aRes += temp[i].w; + } + + 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; + 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 < kernelSize; 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(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 |
