-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.c
70 lines (56 loc) · 1.05 KB
/
filter.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
#include <stdlib.h>
#include <math.h>
#include "filter.h"
#include "mcc.h"
struct filter_t
{
int x, y;
float map[];
};
struct filter_t *filter_init(int x, int y)
{
struct filter_t *f = malloc(sizeof *f + sizeof *f->map * x * y);
if (f == NULL)
{
LOG("[filter] filter_init(): couldn't allocate %zu bytes\n", sizeof *f + sizeof *f->map * x * y);
return NULL;
}
f->x = x;
f->y = y;
return f;
}
void filter_deinit(struct filter_t *f)
{
free(f);
}
void filter_process(struct filter_t *f, const float *map)
{
int x, y, dx, dy;
LOG("filter: processing\n");
for (x = 0; x < f->x; x++)
{
for (y = 0; y < f->y; y++)
{
float *h = &f->map[x + y * f->x];
int divide = 0;
*h = 0;
for (dx = -1; dx <= 1; dx++)
{
for (dy = -1; dy <= 1; dy++)
{
int ax = x + dx;
int ay = y + dy;
if (ax < 0 || ay < 0 || ax >= f->x || ay >= f->y) continue;
*h += map[ax + ay * f->x];
divide++;
}
}
*h /= divide;
}
}
LOG("filter: complete\n");
}
const float *filter_map(struct filter_t *f)
{
return f->map;
}