-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.c
64 lines (49 loc) · 1.42 KB
/
example.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
#include "MedianFilter.h"
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
static sMedianFilter_t medianFilter;
#define DEBUG
int main(int argc, char** argv)
{
//input
if(argc != 2) {
fprintf(stderr, "Need to specify window size argument\n");
exit(EXIT_FAILURE);
}
uint32_t wndsz;
if (sscanf(argv[1], "%"SCNu32, &wndsz) == -1) {
fprintf(stderr, "Invalid integer\n");
exit(EXIT_FAILURE);
}
if(wndsz % 2 == 0 || wndsz == 0) {
fprintf(stderr, "Window must be odd and positive\n");
exit(2);
}
//initialization
medianFilter.numNodes = wndsz;
medianFilter.medianBuffer = (sMedianNode_t*) calloc(sizeof(sMedianNode_t), wndsz);
if (!medianFilter.medianBuffer) {
fprintf(stderr, "Memory error\n");
exit(EXIT_FAILURE);
}
if (MEDIANFILTER_Init(&medianFilter) == -1) {
fprintf(stderr, "medianFilter initialization error\n");
exit(EXIT_FAILURE);
}
// filtering
uint32_t slice;
while(fread(&slice, sizeof(uint32_t), 1, stdin) > 0)
{
int medianValue = MEDIANFILTER_Insert(&medianFilter, slice);
fwrite(&medianValue, sizeof(uint32_t), 1, stdout);
#ifdef DEBUG
fprintf(stderr, "New value: %d \tMedian value: %d\r\n", slice, medianValue);
// sleep(1);
#endif
}
fprintf(stderr, "\n");
// free memory
free(medianFilter.medianBuffer);
return 0;
}