-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.c
More file actions
189 lines (169 loc) · 5.25 KB
/
Copy pathfilter.c
File metadata and controls
189 lines (169 loc) · 5.25 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
182
183
184
185
186
187
188
189
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "bmp.h"
// Prototypes
void red(int height, int width, RGB img[height][width]);
void green(int height, int width, RGB img[height][width]);
void blue(int height, int width, RGB img[height][width]);
int main(int argc, char *argv[]) {
// Check command validity
char *filters = "rgb";
char *choice = argv[1];
if (argc != 4 || strlen(choice) != 1 || strchr(filters, *choice) == NULL)
{
printf("Usage: ./filter <r/g/b> infile.bmp outfile.bmp\n");
return 1;
}
// Received file names
char *in_name = argv[2];
char *out_name = argv[3];
// Open input image & output file
FILE *infile = fopen(in_name, "r");
if (infile == NULL)
{
printf("Could not process. Check your infile.\n");
return 2;
}
FILE *outfile = fopen(out_name, "w");
if (outfile == NULL)
{
fclose(infile);
printf("Could not process. Check your outfile.\n");
return 3;
}
// Check BMP file header and info header
FILEHEADER fh;
fread(&fh, sizeof(FILEHEADER), 1, infile);
INFOHEADER ih;
fread(&ih, sizeof(INFOHEADER), 1, infile);
// Check BMP format - implementation from Harvard's CS50 course - pset 4
if (fh.bfType != 0x4d42 || fh.bfOffBits != 54 || ih.biSize != 40 || ih.biBitCount != 24 || ih.biCompression != 0)
{
fclose(infile);
fclose(outfile);
printf("Supported BMP format: 24-bit uncompressed BMP 4.0");
return 4;
}
// Memory allocation for the image
int height = abs(ih.biHeight);
int width = ih.biWidth;
RGB (*img)[width] = calloc(height, width * sizeof(RGB));
if (img == NULL)
{
printf("Could not process. Not enough memory.\n");
fclose(infile);
fclose(outfile);
return 5;
}
// Padding implementation from Harvard's CS50 course - pset 4
int padding = (4 - (width * sizeof(RGB)) % 4) % 4;
// Iterate through the input image - rows
for (int i = 0; i < height; i++)
{
fread(img[i], sizeof(RGB), width, infile);
fseek(infile, padding, SEEK_CUR);
}
// Choose filter
switch (*choice) {
case 'r':
red(height, width, img);
break;
case 'g':
green(height, width, img);
break;
case 'b':
blue(height, width, img);
break;
}
// Write output header values on outfile.bmp
fwrite(&fh, sizeof(FILEHEADER), 1, outfile);
fwrite(&ih, sizeof(INFOHEADER), 1, outfile);
// Write new pixel values
for (int j = 0; j < height; j++)
{
fwrite(img[j], sizeof(RGB), width, outfile);
// Padding
for (int k = 0; k < padding; k++)
{
fputc(0x00, outfile);
}
}
// Free memory & Close files
free(img);
fclose(infile);
fclose(outfile);
return 0;
}
// Functions (R, G, B)
void red(int height, int width, RGB img[height][width])
{
// Iterate through the rows
for (int i = 0; i < height; i++)
{
// Replace each pixel with a new value
for (int j = 0; j < width; j++)
{
// Evaluate values
float value_checker;
value_checker = (img[i][j].rgbBlue + img[i][j].rgbGreen) / 2.0;
if (img[i][j].rgbRed - value_checker < 75)
{
// Apply with new values
float new_grey = (img[i][j].rgbRed + img[i][j].rgbGreen + img[i][j].rgbBlue) / 3.0;
img[i][j].rgbRed = round(new_grey);
img[i][j].rgbGreen = round(new_grey);
img[i][j].rgbBlue = round(new_grey);
}
}
}
return;
}
void green(int height, int width, RGB img[height][width])
{
// Iterate through the rows
for (int i = 0; i < height; i++)
{
// Replace each pixel with a new value
for (int j = 0; j < width; j++)
{
// Evaluate values
float value_checker;
value_checker = (img[i][j].rgbRed + img[i][j].rgbBlue) / 2.0;
if (img[i][j].rgbGreen - value_checker < 75)
{
// Apply with new values
float new_grey = (img[i][j].rgbRed + img[i][j].rgbGreen + img[i][j].rgbBlue) / 3.0;
img[i][j].rgbRed = round(new_grey);
img[i][j].rgbGreen = round(new_grey);
img[i][j].rgbBlue = round(new_grey);
}
}
}
return;
}
void blue(int height, int width, RGB img[height][width])
{
// Iterate through the rows
for (int i = 0; i < height; i++)
{
// Replace each pixel with a new value
for (int j = 0; j < width; j++)
{
// Evaluate values
float value_checker;
value_checker = (img[i][j].rgbRed + img[i][j].rgbGreen) / 2.0;
if (img[i][j].rgbBlue - value_checker < 75)
{
// Apply with new values
float new_grey = (img[i][j].rgbRed + img[i][j].rgbGreen + img[i][j].rgbBlue) / 3.0;
img[i][j].rgbRed = round(new_grey);
img[i][j].rgbGreen = round(new_grey);
img[i][j].rgbBlue = round(new_grey);
}
}
}
return;
}