Skip to content

Commit a6c6d37

Browse files
Merge pull request #10 from indrasuthar07/main
feat: adding colour inversion image filter functionality in helpers.c
2 parents eca5cf5 + e774181 commit a6c6d37

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

filter.c

Lines changed: 7 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 = "bgrs";
10+
char *filters = "bgrsi";
1111

1212
// Get filter flag and check validity
1313
char filter = getopt(argc, argv, filters);
@@ -119,6 +119,12 @@ int main(int argc, char *argv[])
119119
case 's':
120120
sepia(height, width, image);
121121
break;
122+
123+
//invert
124+
case 'i':
125+
invert(height,width,image);
126+
break;
127+
122128
}
123129

124130
// Write outfile's BITMAPFILEHEADER

filter.exe

-61.4 KB
Binary file not shown.

helpers.c

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,53 @@
11
#include "helpers.h"
22

3-
3+
int min(int a,int b){
4+
if(a<b) return a;
5+
return b;
6+
}
7+
int max(int a,int b){
8+
if(a>b) return a;
9+
return b;
10+
}
411
void grayscale(int height, int width, RGBTRIPLE image[height][width]){
5-
12+
return;
13+
614
// Convert image to grayscale
715

816
}
917

18+
void invert(int height, int width, RGBTRIPLE image[height][width]){
19+
for(int i = 0; i<height; i++){
20+
for(int j = 0; j<width; j++){
21+
//Original RGB
22+
int red = image[i][j].rgbtRed;
23+
int green = image[i][j].rgbtGreen;
24+
int blue = image[i][j].rgbtBlue;
25+
26+
//Normal inversion
27+
int invRed = 255 - red;
28+
int invGreen = 255 - green;
29+
int invBlue = 255 - blue;
30+
31+
//for maintain average brightness
32+
int originalBrightness = 0.299*red + 0.587*green + 0.114*blue;
33+
int invertedBrightness = 0.299*invRed + 0.587*invGreen + 0.114*invBlue;
34+
int brightnessDiff = originalBrightness - invertedBrightness;
35+
36+
invRed = min(max(invRed + brightnessDiff, 0), 255);
37+
invGreen = min(max(invGreen + brightnessDiff, 0), 255);
38+
invBlue = min(max(invBlue + brightnessDiff, 0), 255);
39+
40+
//Assign
41+
image[i][j].rgbtRed = invRed;
42+
image[i][j].rgbtGreen = invGreen;
43+
image[i][j].rgbtBlue = invBlue;
44+
}
45+
}
46+
return;
47+
48+
// Convert image to grayscale
1049

50+
}
1151
void sepia(int height, int width, RGBTRIPLE image[height][width]){
1252

1353
// Convert image to sepia

helpers.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// Convert image to grayscale
44
void grayscale(int height, int width, RGBTRIPLE image[height][width]);
55

6+
// invert image
7+
void invert(int height, int width, RGBTRIPLE image[height][width]);
8+
69
// Convert image to sepia
710
void sepia(int height, int width, RGBTRIPLE image[height][width]);
811

output.bmp

68.7 MB
Binary file not shown.

0 commit comments

Comments
 (0)