Skip to content

Commit 9e2b0fb

Browse files
committed
feat: adding blur image function
1 parent 9f34e2d commit 9e2b0fb

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
{
2-
"C_Cpp.errorSquiggles": "disabled"
2+
"C_Cpp.errorSquiggles": "disabled",
3+
"files.associations": {
4+
"helpers.h": "c"
5+
}
36
}

filter.exe

-49.6 KB
Binary file not shown.

helpers.c

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "helpers.h"
2-
2+
#include <stdint.h>
3+
#include <stdlib.h>
4+
#include "bmp.h"
35
int min(int a,int b){
46
if(a<b) return a;
57
return b;
@@ -83,7 +85,44 @@ void reflect(int height, int width, RGBTRIPLE image[height][width]){
8385

8486

8587
void blur(int height, int width, RGBTRIPLE image[height][width]){
88+
// Allocate temporary array on heap
89+
RGBTRIPLE **temp = malloc(height * sizeof(RGBTRIPLE *));
90+
for (int i = 0; i < height; i++)
91+
temp[i] = malloc(width * sizeof(RGBTRIPLE));
92+
93+
int kernelSize = 21; // large kernel for heavy blur
94+
int offset = kernelSize / 2;
95+
// Repeating blur 2-3 times for ultra blur effect
96+
//because in single time effect not much visible
97+
for(int repeat = 0; repeat < 3; repeat++){
98+
for(int i = 0; i < height; i++){
99+
for (int j = 0; j < width; j++){
100+
int sumRed = 0, sumGreen = 0, sumBlue = 0;
101+
int count = 0;
102+
for (int ki = -offset; ki <= offset; ki++){
103+
for(int kj = -offset; kj <= offset; kj++){
104+
int ni = i + ki;
105+
int nj = j + kj;
106+
if(ni >= 0 && ni < height && nj >= 0 && nj < width){
107+
sumRed += image[ni][nj].rgbtRed;
108+
sumGreen += image[ni][nj].rgbtGreen;
109+
sumBlue += image[ni][nj].rgbtBlue;
110+
count++;
111+
}
112+
}
113+
}
114+
temp[i][j].rgbtRed = (uint8_t)(sumRed / count);
115+
temp[i][j].rgbtGreen = (uint8_t)(sumGreen / count);
116+
temp[i][j].rgbtBlue = (uint8_t)(sumBlue / count);
117+
}
118+
}
119+
// Copy blurred array back to orig
120+
for (int i = 0; i < height; i++)
121+
for (int j = 0; j < width; j++)
122+
image[i][j] = temp[i][j];
123+
}
124+
for (int i = 0; i < height; i++)
125+
free(temp[i]);
126+
free(temp);
127+
}
86128

87-
// Blur image
88-
89-
}
68.7 MB
Binary file not shown.

0 commit comments

Comments
 (0)