-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_mean_filt.c
70 lines (61 loc) · 1.75 KB
/
example_mean_filt.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
/*
* C library computing the partial sums of a 2D matrix in O(m*n) time.
* Query operations run in constant time.
*
* Author: Christopher W. Schankula
* Last updated: December 16th, 2017
* Code is licensed under the CC BY 4.0 license.
*
* THE CODE PROVIDED HEREIN IS PROVIDED "AS-IS"
* THE AUTHOR ASSUMES NO RESPONSBILITY OR WARRANTY
* FOR USE OF THE CODE PROVIDED HEREIN
* EXTENSIVE TESTING OF THIS CODE IS REQUIRED
* TO ENSURE ITS CORRECTNESS AS PART OF THE INTENDED
* APPLICATION THEREOF
*/
#include <stdio.h>
#include "partialsums.h"
#include <stdlib.h>
#define H 8
#define W 8
#define S 3
unsigned int picture[64] =
{
255,255,255,255,255,255,255,255
,255,255,255,255,255,255,255,255
,255, 0 ,255,255,255,255, 0 ,255
,255,255,255,255,255,255,255,255
,255,255,255,255,255,255,255,255
,255, 0 ,255,255,255,255, 0 ,255
,255,255, 0 , 0 , 0 , 0 ,255,255
,255,255,255,255,255,255,255,255
};
int main(){
unsigned long int *sum_lookup = generate_lookup(picture, 8, 8);
unsigned int *new_picture = malloc(H * W * sizeof(int));
printf("Original picture\n");
for (int j = 0; j < H; j++){
for (int i = 0; i < W; i++)
printf("%5d", picture[j*W + i]);
printf("\n");
}
for (int j = 0; j < H; j++){
for (int i = 0; i < W; i++){
/* Handle edge cases */
int x1 = i - S / 2 > 0 ? i - S / 2 : 0;
int y1 = j - S / 2 > 0 ? j - S / 2 : 0;
int x2 = i + S / 2 < W ? i + S / 2 : W - 1;
int y2 = j + S / 2 < H ? j + S / 2 : H - 1;
*(new_picture + j * W + i) = query_sum(sum_lookup, W, x1, y1, x2, y2) / ((x2 - x1 + 1) * (y2 - y1 + 1));
}
}
printf("New picture\n");
for (int j = 0; j < H; j++){
for (int i = 0; i < W; i++)
printf("%5d", new_picture[j*W + i]);
printf("\n");
}
free(new_picture);
free(sum_lookup);
return 0;
}