33#include <stdlib.h>
44#include <math.h>
55#include "bmp.h"
6+ #include <string.h>
7+
8+ #define LEVELS 16
9+ #define RADIUS 8
10+
611int min (int a ,int b ){
712 if (a < b ) return a ;
813 return b ;
@@ -11,6 +16,7 @@ int max(int a,int b){
1116 if (a > b ) return a ;
1217 return b ;
1318}
19+
1420void grayscale (int height , int width , RGBTRIPLE image [height ][width ]){
1521
1622 for (int i = 0 ; i < height ; i ++ ){
@@ -356,3 +362,64 @@ void glow(int height, int width, RGBTRIPLE image[height][width])
356362 free (original );
357363 free (blurred );
358364}
365+ // Oil Paint filter
366+ void oilpaint (int height , int width , RGBTRIPLE image [height ][width ]){
367+ RGBTRIPLE * * copy = malloc (height * sizeof (RGBTRIPLE * ));
368+ for (int i = 0 ; i < height ; i ++ ){
369+ copy [i ] = malloc (width * sizeof (RGBTRIPLE ));
370+ for (int j = 0 ; j < width ; j ++ ){
371+ copy [i ][j ] = image [i ][j ];
372+ }
373+ }
374+ // logic
375+ for (int i = 0 ; i < height ; i ++ ){
376+ for (int j = 0 ; j < width ; j ++ ){
377+ int intensityCount [LEVELS ];
378+ long sumRed [LEVELS ], sumGreen [LEVELS ], sumBlue [LEVELS ];
379+ memset (intensityCount , 0 , sizeof (intensityCount ));
380+ memset (sumRed , 0 , sizeof (sumRed ));
381+ memset (sumGreen , 0 , sizeof (sumGreen ));
382+ memset (sumBlue , 0 , sizeof (sumBlue ));
383+
384+ // Analyze neighborhood
385+ for (int di = - RADIUS ; di <= RADIUS ; di ++ ){
386+ for (int dj = - RADIUS ; dj <= RADIUS ; dj ++ ){
387+ int ni = i + di ;
388+ int nj = j + dj ;
389+ if (ni < 0 || ni >= height || nj < 0 || nj >= width )
390+ continue ;
391+
392+ RGBTRIPLE pixel = copy [ni ][nj ];
393+ int intensity = ((pixel .rgbtRed + pixel .rgbtGreen + pixel .rgbtBlue ) / 3 ) * LEVELS / 256 ;
394+ if (intensity >= LEVELS )
395+ intensity = LEVELS - 1 ;
396+
397+ intensityCount [intensity ]++ ;
398+ sumRed [intensity ] += pixel .rgbtRed ;
399+ sumGreen [intensity ] += pixel .rgbtGreen ;
400+ sumBlue [intensity ] += pixel .rgbtBlue ;
401+ }
402+ }
403+
404+ int dominant = 0 , maxCount = 0 ;
405+ for (int k = 0 ; k < LEVELS ; k ++ ){
406+ if (intensityCount [k ] > maxCount ){
407+ maxCount = intensityCount [k ];
408+ dominant = k ;
409+ }
410+ }
411+ if (maxCount > 0 ){
412+ int newRed = sumRed [dominant ] / maxCount ;
413+ int newGreen = sumGreen [dominant ] / maxCount ;
414+ int newBlue = sumBlue [dominant ] / maxCount ;
415+
416+ image [i ][j ].rgbtRed = (newRed / 32 ) * 32 ;
417+ image [i ][j ].rgbtGreen = (newGreen / 32 ) * 32 ;
418+ image [i ][j ].rgbtBlue = (newBlue / 32 ) * 32 ;
419+ }
420+ }
421+ }
422+ for (int i = 0 ; i < height ; i ++ )
423+ free (copy [i ]);
424+ free (copy );
425+ }
0 commit comments