-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_iplib.c
187 lines (184 loc) · 6.25 KB
/
test_iplib.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* CIMP - C Image Manipulation Program
* (https://github.com/EPMatt/cimp)
*
* (C) 2020
*
* Ina Popescu (https://github.com/ina-pps)
* Matteo Agnoletto (https://github.com/EPMatt)
* Lorenzo Donatelli (https://github.com/whitedemond)
*
* For licensing conditions related to this project, see LICENSE
*
* test_iplib.c - Programma di test per la libreria iplib. Esegue tutte le funzioni della libreria in sequenza.
* Pensato per essere eseguito con l'utility CLI valgrind.
*/
#include <stdio.h>
#include "ip_lib.h"
#include "bmp.h"
#include "time.h"
#define ROUNDS_NUM 10 /* number of tests iterations */
#define MAT_W 100
#define MAT_H 200
#define MAT_CHANNELS 3
#define TOTAL_TESTS_NUM MAT_CHANNELS + 23
int main()
{
int round;
ip_mat **round_arr;
/* init random */
srand(time(NULL));
for (round = 1; round <= ROUNDS_NUM; round++)
{
channel_t a, b;
ip_mat **pt, *original_pt, *a_pt, *b_pt, *a_filter, *padded;
int row, col, ch, size_w, size_h;
float foo_mean;
printf("TEST No.%d: Start...\n", round);
/* generate random sizes */
size_h = rand() % MAT_H + 1;
size_w = rand() % MAT_W + 1;
printf("\tGenerated random sizes -> height: %d, width: %d\n", size_h, size_w);
round_arr = (ip_mat **)malloc(sizeof(ip_mat *) * (TOTAL_TESTS_NUM));
pt = round_arr;
/* execute all the exposed library functions (those declared in the header file) */
/* PART 1: mem */
printf("\tip_mat_create...\n");
*pt = ip_mat_create(size_h, 100, MAT_CHANNELS, 0.0);
/* test get_val & set_val */
printf("\tget_val & set_val...\n");
for (ch = 0; ch < (*pt)->k; ch++)
{
for (row = 0; row < (*pt)->h; row++)
{
for (col = 0; col < (*pt)->w; col++)
{
/* silly get & set test */
set_val(*pt, row, col, ch, 1.0);
}
}
}
printf("\tcompute_stats...\n");
compute_stats(*pt);
original_pt = *pt;
pt++;
printf("\tip_mat_init_random...\n");
ip_mat_init_random(original_pt, 10.0, 0.5);
printf("\tip_mat_copy...\n");
*pt = ip_mat_copy(original_pt);
pt++;
printf("\tip_mat_subset...\n");
*pt = ip_mat_subset(original_pt, 0, original_pt->h, 0, original_pt->w);
a_pt = original_pt;
b_pt = original_pt;
pt++;
/* test concat on all channels */
printf("\tip_mat_concat...\n");
for (ch = 0; ch < a_pt->k; ch++)
{
*pt = ip_mat_concat(a_pt, b_pt, ch);
pt++;
}
/* PART 1: Math */
printf("\tip_mat_sum...\n");
*pt = ip_mat_sum(a_pt, b_pt);
pt++;
printf("\tip_mat_sub...\n");
*pt = ip_mat_sub(a_pt, b_pt);
pt++;
printf("\tip_mat_mul_scalar...\n");
*pt = ip_mat_mul_scalar(original_pt, 10.0);
pt++;
printf("\tip_mat_add_scalar...\n");
*pt = ip_mat_add_scalar(original_pt, 10.0);
pt++;
printf("\tip_mat_mean...\n");
*pt = ip_mat_mean(a_pt, b_pt);
pt++;
/* PART 2 */
printf("\tip_mat_to_gray_scale...\n");
*pt = ip_mat_to_gray_scale(original_pt);
pt++;
printf("\tip_mat_blend...\n");
*pt = ip_mat_blend(a_pt, b_pt, 0.5);
pt++;
printf("\tip_mat_brighten...\n");
*pt = ip_mat_brighten(original_pt, 0.5);
pt++;
printf("\tip_mat_corrupt...\n");
*pt = ip_mat_corrupt(original_pt, 2.0);
pt++;
/* PART 3 */
printf("\tip_mat_padding...\n");
*pt = ip_mat_padding(original_pt, 1, 1);
padded = *pt;
pt++;
/* first make some filters */
printf("\tcreate_sharpen_filter...\n");
*pt = create_sharpen_filter();
a_filter = *pt;
pt++;
printf("\tcreate_edge_filter...\n");
*pt = create_edge_filter();
pt++;
printf("\tcreate_emboss_filter...\n");
*pt = create_emboss_filter();
pt++;
printf("\tcreate_average_filter...\n");
*pt = create_average_filter(3, 3, 3);
pt++;
printf("\tcreate_gaussian_filter...\n");
*pt = create_gaussian_filter(3, 3, 3, 0.5);
pt++;
/* test convolve: all filters */
printf("\tip_mat_convolve (sharpen_filter)...\n");
*pt = ip_mat_convolve(padded, *(pt - 5));
pt++;
printf("\tip_mat_convolve (edge_filter)...\n");
*pt = ip_mat_convolve(padded, *(pt - 5));
pt++;
printf("\tip_mat_convolve (emboss_filter)...\n");
*pt = ip_mat_convolve(padded, *(pt - 5));
pt++;
printf("\tip_mat_convolve (average_filter)...\n");
*pt = ip_mat_convolve(padded, *(pt - 5));
pt++;
printf("\tip_mat_convolve (gaussian_filter)...\n");
*pt = ip_mat_convolve(padded, *(pt - 5));
/* clamp & rescale on last ip_mat */
printf("\tclamp...\n");
clamp(*pt, 50.0, 75.0);
printf("\trescale...\n");
rescale(*pt, 100.0);
/* HELPERS */
printf("\tget_channel...\n");
a = get_channel(padded, 0);
b = get_channel(padded, 1);
printf("\tchannel_puts...\n");
channel_puts(b, a, 0, 0);
ip_mat_puts(b_pt, a_pt, 0, 0, NO_COMPUTE_STATS);
printf("\tnot_null_ip_mat...\n");
not_null_ip_mat(a_pt);
printf("\tmin...\n");
min(a_pt, 0);
printf("\tmax...\n");
max(a_pt, 0);
printf("\tmean...\n");
foo_mean = mean(a_pt, 0);
printf("\trestrict_val...\n");
restrict_val(foo_mean, MIN_PIXEL_FLOAT, MAX_PIXEL_FLOAT);
printf("\tmean_pixel_channel...\n");
mean_pixel_channel(original_pt, 0, 0);
printf("\tequal_dimension...\n");
equal_dimension(a_pt, b_pt);
printf("\tconvolve_channel...\n");
convolve_channel(a, get_channel(a_filter, 0), 0, 0);
/* free everything */
printf("\tip_mat_free...\n");
for (pt = round_arr; pt < round_arr + TOTAL_TESTS_NUM; pt++)
ip_mat_free(*pt);
printf("\tall tests done! free the round array...\n");
free(round_arr);
printf("TEST No.%d: Done!\n", round);
}
}