Skip to content

Commit 83ea8ea

Browse files
Merge pull request #34 from indrasuthar07/main
feat: adding oil paint effect filter
2 parents 6d8925a + c7db579 commit 83ea8ea

File tree

6 files changed

+74
-1
lines changed

6 files changed

+74
-1
lines changed

filter.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
int main(int argc, char *argv[])
88
{
99
// Define allowable filters
10-
char *filters = "bgrsivtdGB:";
10+
char *filters = "bgrsivtdGoB:";
1111

1212

1313
char filterArr[argc-3];
@@ -146,6 +146,10 @@ int main(int argc, char *argv[])
146146
case 'G':
147147
glow(height, width, image);
148148
break;
149+
case 'o':
150+
oilpaint(height, width, image);
151+
break;
152+
149153
default:
150154
printf("Unknown filter: %c\n", filterArr[i]);
151155
free(image);

filter.exe

113 KB
Binary file not shown.

helpers.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
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+
611
int 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+
1420
void 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+
}

helpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ void vignette(int height, int width, RGBTRIPLE image[height][width]);
3535

3636
// Glow filter
3737
void glow(int height, int width, RGBTRIPLE image[height][width]);
38+
// Oil Paint filter
39+
void oilpaint(int height, int width, RGBTRIPLE image[height][width]);
3840
#endif

input.bmp renamed to in.bmp

File renamed without changes.

out.bmp

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)