Skip to content

Commit 9d98a18

Browse files
committed
feat: added combined filter support
1 parent bf737f7 commit 9d98a18

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

.github/Contributor_Guide/Project_Tour.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ Why are these structs useful? Well, recall that a file is just a sequence of byt
1414

1515

1616
### filter.c
17-
1817
Now, let’s open up filter.c. This file has been written already for you, but there are a couple important points worth noting here.
1918

2019
First, notice the definition of filters on line 10. That string tells the program what the allowable command-line arguments to the program are: b, g, r, and s. Each of them specifies a different filter that we might apply to our images: blur, grayscale, reflection, and sepia.
2120

21+
Then, lines 11 to 32 run a check for flags and validates them while storing them up in an array for sequentially applying them.
22+
2223
The next several lines open up an image file, make sure it’s indeed a BMP file, and read all of the pixel information into a 2D array called image.
2324

24-
Scroll down to the switch statement that begins on line 101. Notice that, depending on what filter we’ve chosen, a different function is called: if the user chooses filter b, the program calls the blur function; if g, then grayscale is called; if r, then reflect is called; and if s, then sepia is called. Notice, too, that each of these functions take as arguments the height of the image, the width of the image, and the 2D array of pixels.
25+
Scroll down to the for loop on switch statement that begins on line 101. Notice that, depending on what filter flags you've passed in sequence , a different function is called: if the user chooses filter b, the program calls the blur function; if g, then grayscale is called; if r, then reflect is called; and if s, then sepia is called. Notice, too, that each of these functions take as arguments the height of the image, the width of the image, and the 2D array of pixels. After each execution of filter function, loop moves onto next filter in array to apply, if present.
2526

2627
These are the functions you’ll (soon!) implement. As you might imagine, the goal is for each of these functions to edit the 2D array of pixels in such a way that the desired filter is applied to the image.
2728

2829
The remaining lines of the program take the resulting image and write them out to a new image file.
2930

30-
3131
### Helpers.h
3232

3333
Next, take a look at helpers.h. This file is quite short, and just provides the function prototypes for the functions you saw earlier.
@@ -63,3 +63,6 @@ Paste This in you Terminal(without quotes)
6363
4. For sepia:
6464

6565
"filter -s in.bmp out.bmp"
66+
5. To run multiple filters:
67+
68+
"filter -g -s -i in.bmp out.bmp"

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,16 @@ Now, let’s open up filter.c. This file has been written already for you, but t
4747

4848
First, notice the definition of filters on line 10. That string tells the program what the allowable command-line arguments to the program are: b, g, r, and s. Each of them specifies a different filter that we might apply to our images: blur, grayscale, reflection, and sepia.
4949

50+
Then, lines 11 to 32 run a check for flags and validates them while storing them up in an array for sequentially applying them.
51+
5052
The next several lines open up an image file, make sure it’s indeed a BMP file, and read all of the pixel information into a 2D array called image.
5153

52-
Scroll down to the switch statement that begins on line 101. Notice that, depending on what filter we’ve chosen, a different function is called: if the user chooses filter b, the program calls the blur function; if g, then grayscale is called; if r, then reflect is called; and if s, then sepia is called. Notice, too, that each of these functions take as arguments the height of the image, the width of the image, and the 2D array of pixels.
54+
Scroll down to the for loop on switch statement that begins on line 101. Notice that, depending on what filter flags you've passed in sequence , a different function is called: if the user chooses filter b, the program calls the blur function; if g, then grayscale is called; if r, then reflect is called; and if s, then sepia is called. Notice, too, that each of these functions take as arguments the height of the image, the width of the image, and the 2D array of pixels. After each execution of filter function, loop moves onto next filter in array to apply, if present.
5355

5456
These are the functions you’ll (soon!) implement. As you might imagine, the goal is for each of these functions to edit the 2D array of pixels in such a way that the desired filter is applied to the image.
5557

5658
The remaining lines of the program take the resulting image and write them out to a new image file.
5759

58-
5960
### Helpers.h
6061

6162
Next, take a look at helpers.h. This file is quite short, and just provides the function prototypes for the functions you saw earlier.

filter.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,23 @@ int main(int argc, char *argv[])
99
// Define allowable filters
1010
char *filters = "bgrsi";
1111

12-
// Get filter flag and check validity
13-
char filter = getopt(argc, argv, filters);
14-
if (filter == '?')
15-
{
16-
printf("Invalid filter.\n");
17-
return 1;
18-
}
19-
20-
// Ensure only one filter
21-
if (getopt(argc, argv, filters) != -1)
22-
{
23-
printf("Only one filter allowed.\n");
24-
return 2;
12+
13+
char filterArr[argc-3];
14+
15+
// 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");
22+
return 1;
23+
}
2524
}
25+
2626

2727
// Ensure proper usage
28-
if (argc != optind + 2)
28+
if (argc < optind + 2)
2929
{
3030
printf("Usage: ./filter [flag] infile outfile\n");
3131
return 3;
@@ -63,7 +63,7 @@ int main(int argc, char *argv[])
6363
// Ensure infile is (likely) a 24-bit uncompressed BMP 4.0
6464
if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
6565
bi.biBitCount != 24 || bi.biCompression != 0)
66-
{
66+
{
6767
fclose(outptr);
6868
fclose(inptr);
6969
printf("Unsupported file format.\n");
@@ -98,7 +98,8 @@ int main(int argc, char *argv[])
9898
}
9999

100100
// Filter image
101-
switch (filter)
101+
for(int i=0; i<argc-3; i++){
102+
switch (filterArr[i])
102103
{
103104
// Blur
104105
case 'b':
@@ -124,9 +125,12 @@ int main(int argc, char *argv[])
124125
case 'i':
125126
invert(height,width,image);
126127
break;
128+
default:
129+
printf("%c", &filterArr[i]);
130+
break;
127131

128132
}
129-
133+
}
130134
// Write outfile's BITMAPFILEHEADER
131135
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
132136

filter.exe

-476 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)