-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgreyscale.c
executable file
·42 lines (33 loc) · 901 Bytes
/
greyscale.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bitmap.h"
/*
* The greyscale function
*/
void greyscale_filter(Bitmap *bmp) {
int width = bmp->width;
int height = bmp->height;
int n = 0;
for(int i = 0; i < (width*height); i++) {
Pixel *pixel = malloc(sizeof(Pixel));
n = fread(pixel, 1, sizeof(Pixel), stdin);
if (n == -1) {
perror("read");
exit(1);
} else if(n == 0) {
fprintf(stderr, "Error: could not read from stdin");
exit(1);
}
unsigned char new = ((pixel->blue + pixel->green + pixel->red)/3);
pixel->blue = new;
pixel->green = new;
pixel->red = new;
fwrite(pixel, 1, sizeof(Pixel), stdout);
free(pixel);
}
}
int main() {
run_filter(greyscale_filter, 1);
return 0;
}