Skip to content

Commit 367fff3

Browse files
Merge branch 'main' into glow-filter
2 parents 129f220 + fb2260f commit 367fff3

File tree

4 files changed

+49
-33
lines changed

4 files changed

+49
-33
lines changed

README.md

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,14 @@ For a pixel along the edge or corner, like pixel 15, we would still look for all
113113

114114
If you apply the above algorithm to each pixel in the image, the result should look like a blurry, out-of-focus version of the original.
115115

116-
### 5.) The Vignette Algorithm
117-
118-
The vignette filter gives an image a cinematic look by gradually darkening its corners.
119-
120-
**Algorithm:**
121-
1. Compute the center of the image (cx, cy).
122-
2. Calculate the maximum distance from the center to any corner (max_dis).
123-
3. For each pixel at (i, j):
124-
- Calculate the Euclidean distance from this pixel to the center: dist = sqrt((j - cx)^2 + (i - cy)^2)
125-
- Compute a vignette factor: vig = 1.0 - (dist / max_dis)
126-
- Clamp vig between 0.0 (fully dark) and 1.0 (original color).
127-
- Multiply the pixel's R, G, B values by vig to darken farther-away pixels more thoroughly.
128-
129-
If you apply this algorithm, your resulting image will have gently darkened corners and an undisturbed/sunnier center.
116+
### 6.) Brightness Adjustment Filter
117+
- **Flag:** `-B <value>`
118+
- **Description:** Increases or decreases the brightness of the image by adding a fixed value to each pixel's R, G, and B channels. The value should be an integer—positive to increase, negative to decrease.
119+
- **Usage examples:**
120+
```sh
121+
./filter -B 40 input.bmp output.bmp # Increase brightness by 40
122+
./filter -B -30 input.bmp output.bmp # Decrease brightness by 30
123+
```
130124

131125
### 6.) The Threshold Algorithm
132126

filter.c

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77
int main(int argc, char *argv[])
88
{
99
// Define allowable filters
10-
char *filters = "bgrsivtw";
10+
char *filters = "bgrsivB:tw";
1111

1212

1313
char filterArr[argc-3];
14+
int filterCount = 0;
1415

1516
// gets all filter flags and checks validity
16-
for(int i=0; i<argc; i++){
17-
char temp = getopt(argc,argv,filters);
18-
if(temp == -1) break;
19-
filterArr[i]= temp;
20-
if(filterArr[i] == '?') {
21-
printf("Invalid filter option");
17+
int opt;
18+
while ((opt = getopt(argc, argv, filters)) != -1) {
19+
if (opt == '?') {
20+
printf("Invalid filter option\n");
2221
return 1;
2322
}
23+
filterArr[filterCount++] = opt;
2424
}
2525

2626

@@ -98,7 +98,7 @@ int main(int argc, char *argv[])
9898
}
9999

100100
// Filter image
101-
for(int i=0; i<argc-3; i++){
101+
for(int i=0; i<filterCount; i++){
102102
switch (filterArr[i])
103103
{
104104
// Blur
@@ -124,27 +124,30 @@ int main(int argc, char *argv[])
124124
case 'i':
125125
invert(height,width,image);
126126
break;
127+
127128
// Vignette
128129
case 'v':
129130
vignette(height, width, image);
130131
break;
131-
case 't':
132-
threshold(height, width, image);
132+
133+
// Brightness Adjust
134+
case 'B': {
135+
int brightness_value = atoi(optarg);
136+
brightness(height, width, image, brightness_value);
133137
break;
134138
case 'w':
135139
glow(height, width, image);
136140
break;
141+
}
137142
default:
138-
printf("%c", &filterArr[i]);
143+
printf("Unknown filter: %c\n", filterArr[i]);
139144
break;
140145

141146
}
142-
}
147+
143148
// Write outfile's BITMAPFILEHEADER
144149
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
145150

146-
// Write outfile's BITMAPINFOHEADER
147-
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
148151

149152
// Write new pixels to outfile
150153
for (int i = 0; i < height; i++)

helpers.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,24 @@ void blur(int height, int width, RGBTRIPLE image[height][width]){
149149
free(temp);
150150
}
151151

152-
// Blur image
153-
152+
void brightness(int height, int width, RGBTRIPLE image[height][width], int value) {
153+
for (int i = 0; i < height; i++) {
154+
for (int j = 0; j < width; j++) {
155+
int r = image[i][j].rgbtRed + value;
156+
int g = image[i][j].rgbtGreen + value;
157+
int b = image[i][j].rgbtBlue + value;
158+
if (r > 255) r = 255;
159+
if (g > 255) g = 255;
160+
if (b > 255) b = 255;
161+
if (r < 0) r = 0;
162+
if (g < 0) g = 0;
163+
if (b < 0) b = 0;
164+
image[i][j].rgbtRed = r;
165+
image[i][j].rgbtGreen = g;
166+
image[i][j].rgbtBlue = b;
167+
}
168+
}
169+
}
154170

155171
void vignette(int height, int width, RGBTRIPLE image[height][width]){
156172
float cx = width / 2.0; // center of the image

helpers.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ void reflect(int height, int width, RGBTRIPLE image[height][width]);
1414

1515
// Blur image
1616
void blur(int height, int width, RGBTRIPLE image[height][width]);
17-
// Vignette image
18-
void vignette(int height, int width, RGBTRIPLE image[height][width]);
1917

2018
//Threshold Filter(Black & White)
2119
void threshold(int height, int width, RGBTRIPLE image[height][width]);
2220

2321
// Glow filter
24-
void glow(int height, int width, RGBTRIPLE image[height][width]);
22+
void glow(int height, int width, RGBTRIPLE image[height][width]);
23+
// Brightness adjustment filter
24+
void brightness(int height, int width, RGBTRIPLE image[height][width], int value);
25+
26+
// Vignette filter
27+
void vignette(int height, int width, RGBTRIPLE image[height][width]);

0 commit comments

Comments
 (0)