-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
84 lines (73 loc) · 2.41 KB
/
main.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
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
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "imageData.h"
#include "mp2.h"
const double gMonoMult[3] = { .299, .587, .114 };
int
main (int argc, const char* const argv[])
{
char garbage[2];
int command;
double sigma;
if (4 > argc || 5 < argc) {
fprintf (stderr,
"syntax: %s <input file> <output file> <command #> ...\n",
argv[0]);
return 2;
}
if (1 != sscanf (argv[3], "%d%1s", &command, garbage) ||
1 > command || 3 < command) {
fprintf (stderr, "Command must be from 1 to 3.\n");
fprintf (stderr, " 1 -- gaussian filter <sigma>\n");
fprintf (stderr, " 2 -- greyscale\n");
fprintf (stderr, " 3 -- invert colors\n");
return 2;
}
if (1 == command &&
(5 != argc ||
1 != sscanf (argv[4], "%lf%1s", &sigma, garbage) ||
0 >= sigma || 100 <= sigma)) {
fprintf (stderr, "Sigma must be greater than 0 and less than 100.\n");
return 2;
}
Image* inputImage = decode (argv[1]);
printf ("Width: %d, height: %d\n", inputImage->width, inputImage->height);
Image* outputImage = generateOutput (inputImage);
int height = inputImage->height;
int width = inputImage->width;
uint8_t* inRed = inputImage->redChannel;
uint8_t* inBlue = inputImage->blueChannel;
uint8_t* inGreen = inputImage->greenChannel;
uint8_t* inAlpha = inputImage->alphaChannel;
uint8_t* outRed = outputImage->redChannel;
uint8_t* outBlue = outputImage->blueChannel;
uint8_t* outGreen = outputImage->greenChannel;
uint8_t* outAlpha = outputImage->alphaChannel;
switch (command) {
case 1: {
int radius = ceil (3 * sigma);
int fSize = 1 + 2 * radius;
double* gauss = malloc (sizeof (gauss[0]) * fSize * fSize);
gaussianFilter (gauss, sigma);
convolveImage (width, height, inRed, inGreen, inBlue, inAlpha,
radius, gauss, outRed, outGreen, outBlue, outAlpha);
encode (argv[2], outputImage);
break;
}
case 2:
greyscale (width, height, inRed, inGreen, inBlue, inAlpha,
gMonoMult, outRed, outGreen, outBlue, outAlpha);
encode (argv[2], outputImage);
break;
case 3:
invertColors (width, height, inRed, inGreen, inBlue, inAlpha,
outRed, outGreen, outBlue, outAlpha);
encode (argv[2], outputImage);
break;
}
freeImage (inputImage);
freeImage (outputImage);
return 0;
}