Skip to content

Commit 9e8f70d

Browse files
modified the files with required changes
1 parent 2d9f77a commit 9e8f70d

File tree

4 files changed

+68
-44
lines changed

4 files changed

+68
-44
lines changed

filter.c

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,30 @@
77
int main(int argc, char *argv[])
88
{
99
// Define allowable filters
10-
char *filters = "bgrsivtd";
11-
12-
10+
char *filters = "bgrsivtdB:";
1311

1412

15-
char filterArr[argc - 3];
16-
int filterCount = 0;
17-
18-
for(int i=0; i<argc; i++){
19-
char temp = getopt(argc,argv,filters);
20-
if(temp == -1) break;
21-
filterArr[i]= temp;
22-
if(filterArr[i] == '?') {
23-
printf("Invalid filter option");
24-
return 1;
25-
}
26-
}
27-
13+
char filterArr[argc-3];
14+
int filterCount = 0;
2815

29-
16+
// gets all filter flags and checks validity
17+
int opt;
18+
while ((opt = getopt(argc, argv, filters)) != -1) {
19+
if (opt == '?') {
20+
printf("Invalid filter option\n");
21+
return 1;
22+
}
23+
filterArr[filterCount++] = opt;
24+
}
3025

3126

27+
// Ensure proper usage
28+
if (argc < optind + 2)
29+
{
30+
printf("Usage: ./filter [flag] infile outfile\n");
31+
return 3;
32+
}
33+
3234
// Remember filenames
3335
char *infile = argv[optind];
3436
char *outfile = argv[optind + 1];
@@ -96,7 +98,7 @@ return 1;
9698
}
9799

98100
// Filter image
99-
for(int i=0; i<argc-3; i++){
101+
for(int i=0; i<filterCount; i++){
100102
switch (filterArr[i])
101103
{
102104
// Blur
@@ -122,25 +124,33 @@ return 1;
122124
case 'i':
123125
invert(height,width,image);
124126
break;
127+
125128
// Vignette
126129
case 'v':
127130
vignette(height, width, image);
128131
break;
132+
129133
case 't':
130134
threshold(height, width, image);
131135
break;
132-
133-
case 'd': // Edge Detection
134-
detect_edges(height, width, image);
135-
break;
136-
137-
default:
138-
printf("Unknown filter: %c\n", filterArr[i]);
139-
free(image);
140-
fclose(inptr);
141-
fclose(outptr);
142-
return 7;
143-
136+
137+
case 'd': // Edge Detection
138+
detect_edges(height, width, image);
139+
break;
140+
141+
// Brightness Adjust
142+
case 'B': {
143+
int brightness_value = atoi(optarg);
144+
brightness(height, width, image, brightness_value);
145+
break;
146+
}
147+
default:
148+
printf("Unknown filter: %c\n", filterArr[i]);
149+
free(image);
150+
fclose(inptr);
151+
fclose(outptr);
152+
return 7;
153+
144154
}
145155
}
146156
// Write outfile's BITMAPFILEHEADER
@@ -169,4 +179,4 @@ return 1;
169179
fclose(inptr);
170180
fclose(outptr);
171181
return 0;
172-
}
182+
}

filter.exe

1.11 KB
Binary file not shown.

helpers.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#include "helpers.h"
22
#include <stdint.h>
33
#include <stdlib.h>
4-
#include <math.h>
5-
#include <stdio.h>
64
#include "bmp.h"
75
int min(int a,int b){
86
if(a<b) return a;
@@ -151,8 +149,24 @@ void blur(int height, int width, RGBTRIPLE image[height][width]){
151149
free(temp);
152150
}
153151

154-
// Blur image
155-
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+
}
156170

157171
void vignette(int height, int width, RGBTRIPLE image[height][width]){
158172
float cx = width / 2.0; // center of the image
@@ -200,11 +214,6 @@ void threshold(int height, int width, RGBTRIPLE image[height][width])
200214
}
201215
}
202216
}
203-
204-
205-
// ----------------------
206-
// EDGE DETECTION (SOBEL)
207-
// ----------------------
208217
void detect_edges(int height, int width, RGBTRIPLE image[height][width])
209218
{
210219
// Temporary copy of the image
@@ -274,4 +283,4 @@ void detect_edges(int height, int width, RGBTRIPLE image[height][width])
274283
for (int i = 0; i < height; i++)
275284
free(copy[i]);
276285
free(copy);
277-
}
286+
}

helpers.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,18 @@ void reflect(int height, int width, RGBTRIPLE image[height][width]);
2020

2121
// Blur image
2222
void blur(int height, int width, RGBTRIPLE image[height][width]);
23-
// Vignette image
24-
void vignette(int height, int width, RGBTRIPLE image[height][width]);
2523

2624
//Threshold Filter(Black & White)
2725
void threshold(int height, int width, RGBTRIPLE image[height][width]);
2826

29-
// **New: Edge Detection filter**
27+
// *New: Edge Detection filter*
3028
void detect_edges(int height, int width, RGBTRIPLE image[height][width]);
3129

30+
// Brightness adjustment filter
31+
void brightness(int height, int width, RGBTRIPLE image[height][width], int value);
32+
33+
// Vignette filter
34+
void vignette(int height, int width, RGBTRIPLE image[height][width]);
35+
36+
3237
#endif

0 commit comments

Comments
 (0)