11#include "helpers.h"
2-
2+ #include <stdint.h>
3+ #include <stdlib.h>
4+ #include "bmp.h"
35int 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
8587void 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- }
0 commit comments