generated from OPCODE-Open-Spring-Fest/template
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathfilter.c
More file actions
181 lines (156 loc) · 4.86 KB
/
filter.c
File metadata and controls
181 lines (156 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include "helpers.h"
#include "image_io.h"
int main(int argc, char *argv[])
{
// Define allowable filters
char *filters = "bgrsivtmdGoPB:";
// Allocate filter array
char *filterArr = (char *)malloc((argc - 2) * sizeof(char));
if (!filterArr) {
printf("Memory allocation error.\n");
return 1;
}
int filterCount = 0;
int brightness_value = 0;
// gets all filter flags and checks validity
int opt;
while ((opt = getopt(argc, argv, filters)) != -1) {
if (opt == '?') {
printf("Invalid filter option\n");
free(filterArr);
return 1;
}
if (opt == 'B') {
if (optarg) {
brightness_value = atoi(optarg);
} else {
printf("Brightness filter requires a value. Usage: -B <value>\n");
free(filterArr);
return 1;
}
filterArr[filterCount++] = opt;
} else if (opt == 'm') {
printf("\nAvailable filters:\n");
printf(" -b Blur\n");
printf(" -g Grayscale\n");
printf(" -r Reflect\n");
printf(" -s Sepia\n");
printf(" -i Invert\n");
printf(" -v Vignette\n");
printf(" -t Threshold\n");
printf(" -d Detect edges\n");
printf(" -B <value> Adjust brightness\n");
printf(" -G Glow\n");
printf(" -o Oil paint\n");
printf(" -P Pixelate (mosaic effect)\n");
printf(" -m Show this menu\n\n");
free(filterArr);
return 0;
} else {
filterArr[filterCount++] = opt;
}
}
if (argc < optind + 2)
{
printf("Usage: ./filter [flag] infile outfile\n");
printf("Filters: -g (grayscale), -s (sepia), -r (reflect), -b (blur), -i (invert), -v (vignette), -G (glow), -t (threshold), -d (edge detection), -o (oil paint), -P (pixelate), -B <value> (brightness)\n");
free(filterArr);
return 3;
}
// Remember filenames
char *infile = argv[optind];
char *outfile = argv[optind + 1];
// Read image using new I/O system
ImageData img;
img.pixels = NULL;
img.width = 0;
img.height = 0;
img.format = IMAGE_FORMAT_UNKNOWN;
if (read_image(infile, &img) != 0) {
printf("Could not read %s. Unsupported file format or file not found.\n", infile);
printf("Supported formats: BMP, PNG, JPEG\n");
return 4;
}
// Validate image was read correctly
if (img.pixels == NULL || img.width <= 0 || img.height <= 0 || img.pixels[0] == NULL) {
printf("Error: Invalid image data after reading.\n");
free_image(&img);
return 4;
}
int height = img.height;
int width = img.width;
// Cast pixels to match filter function
RGBTRIPLE(*image)[width] = (RGBTRIPLE(*)[width])img.pixels[0];
// Filter image
for(int i=0; i<filterCount; i++){
switch (filterArr[i])
{
// Blur
case 'b':
blur(height, width, image);
break;
// Grayscale
case 'g':
grayscale(height, width, image);
break;
// Reflection
case 'r':
reflect(height, width, image);
break;
// Sepia
case 's':
sepia(height, width, image);
break;
//invert
case 'i':
invert(height,width,image);
break;
// Vignette
case 'v':
vignette(height, width, image);
break;
case 't':
threshold(height, width, image);
break;
case 'd': // Edge Detection
detect_edges(height, width, image);
break;
// Brightness Adjust
case 'B':
brightness(height, width, image, brightness_value);
break;
case 'G':
glow(height, width, image);
break;
case 'o':
oilpaint(height, width, image);
break;
case 'P': // Pixelate
pixelate(height, width, image);
break;
default:
printf("Unknown filter: %c\n", filterArr[i]);
free_image(&img);
free(filterArr);
return 7;
}
}
// Free filter array
free(filterArr);
// Write image using I/O system
ImageFormat output_format = get_format_from_extension(outfile);
if (output_format == IMAGE_FORMAT_UNKNOWN) {
output_format = img.format;
}
if (write_image(outfile, &img, output_format) != 0) {
printf("Could not write %s.\n", outfile);
free_image(&img);
return 5;
}
// Free memory for image
free_image(&img);
return 0;
}