-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply_filters.cpp
More file actions
293 lines (246 loc) · 6.95 KB
/
apply_filters.cpp
File metadata and controls
293 lines (246 loc) · 6.95 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
Small app for testing anisotropic non-linear diffusion
*/
#define _USE_MATH_DEFINES
#include <memory.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <assert.h>
#include "pngreader.h"
#include "diffusion.h"
#include "connected_components.h"
#include "colourutils.h"
#include <iostream>
#define ITERS 100
// gaussian
void gauss (
double *input,
const unsigned int maxiters,
const unsigned int w,
const unsigned int h)
{
GaussianDiffusion *diff;
unsigned int i, t;
diff = new GaussianDiffusion (w, h, 5);
for (t=0; t<maxiters; t++)
{
fprintf (stdout, "\riteration %03i/%03i...", t, maxiters);
fflush (stdout);
diff->diffusionStep (input);
}
fprintf (stdout, "\rcompleted %i steps\n", maxiters);
delete diff;
}
// non-linear anisotropic diffusion
void nand (
double *input,
const unsigned int maxiters,
const unsigned int w,
const unsigned int h)
{
AnisotropicDiffusion *diff = NULL;
unsigned int i, t;
char buf[256];
diff = new AnisotropicDiffusion (w, h, 0.12f, 0.02f);
for (t=0; t<maxiters; t++)
{
fprintf (stdout, "\riteration %03i/%03i...", t, maxiters);
fflush (stdout);
diff->diffusionStep (input);
}
fprintf (stdout, "\rcompleted %i steps\n", maxiters);
delete diff;
}
void connected_components (
unsigned char *input,
const unsigned int w,
const unsigned int h)
{
ConnectedComponents *cc = NULL;
unsigned int label_count;
unsigned int i;
unsigned int *labels = NULL;
unsigned char *clr = NULL;
float rgb[3], hsv[3];
cc = new ConnectedComponents (w, h);
labels = new unsigned int[w*h];
// do the connected components
label_count = cc->produceLabeling (input, labels);
for (i=0; i<w*h; i++)
{
hsv[0] = (float)((((double)labels[i])/((double)(label_count)))*360.0);
hsv[1] = 1.0f;
hsv[2] = 1.0f;
ColourUtils::hsv2rgb (hsv, rgb);
input[(i*4)+0] = static_cast<unsigned char>(rgb[0] * 255.0f);
input[(i*4)+1] = static_cast<unsigned char>(rgb[1] * 255.0f);
input[(i*4)+2] = static_cast<unsigned char>(rgb[2] * 255.0f);
input[(i*4)+3] = 255;
}
// write the labels to disk
/*
FILE *f;
sprintf (buf, "LABEL-%03i.label", t);
f = fopen (buf, "wb");
fwrite (labels, sizeof (unsigned int), w*h, f);
fclose (f);
*/
delete cc;
delete[] labels;
delete[] clr;
}
class app_state_t
{
public:
const char *image_filename;
const char *output_filename;
unsigned int iterations;
bool aniso_diffusion;
bool iso_diffusion;
bool connected_components;
public:
app_state_t ()
{
image_filename = NULL;
output_filename = NULL;
iterations = 1;
aniso_diffusion = false;
iso_diffusion = false;
connected_components = false;
}
bool is_valid ()
{
if ((image_filename == NULL) ||
(output_filename == NULL) ||
(iterations == 0) ||
((aniso_diffusion == false) && (iso_diffusion == false) && (connected_components == false)) ||
((aniso_diffusion == true) && (iso_diffusion == true)))
{
// FIXME: check we only have one of aniso, iso and ccoms
return false;
}
return true;
}
void help (const char *app_name)
{
fprintf (stdout, "%s : -i <input image filename> -o <output image filename> -n <number iterations> -a -g\n");
}
void parse_args (int argc, char **argv)
{
for (int i=0; i<argc; i++)
{
if ((strcmp (argv[i], "-i") == 0) ||
(strcmp (argv[i], "--input") == 0))
{
image_filename = argv[++i];
}
else if ((strcmp (argv[i], "-o") == 0) ||
(strcmp (argv[i], "--output") == 0))
{
output_filename = argv[++i];
}
else if ((strcmp (argv[i], "-n") == 0) ||
(strcmp (argv[i], "--iterations") == 0))
{
iterations = atoi (argv[++i]);
}
else if ((strcmp (argv[i], "-a") == 0) ||
(strcmp (argv[i], "-aniso") == 0))
{
aniso_diffusion = true;
}
else if ((strcmp (argv[i], "-g") == 0) ||
(strcmp (argv[i], "--iso") == 0))
{
iso_diffusion = true;
}
else if ((strcmp (argv[i], "-h") == 0) ||
(strcmp (argv[i], "--help") == 0))
{
help (argv[0]);
}
else if ((strcmp (argv[i], "-c") == 0) ||
(strcmp (argv[i], "--cc") == 0))
{
connected_components = true;
}
}
}
};
// main
int main (int argc, char** argv)
{
unsigned int w, h, ct, bd, i;
double *img;
unsigned char *inp, *oup;
app_state_t app_state;
app_state.parse_args (argc, argv);
if (app_state.is_valid () == false)
{
fprintf (stdout, "ERR: invalid parameter combination\n");
return -1;
}
if (PNGIO::readPixelBuffer (
app_state.image_filename,
NULL, w, h, bd, ct) != PNGIO::ERR_NOERR)
{
fprintf (stdout, "ERR: Could not open '%s'\n", app_state.image_filename);
return -2;
}
if (ct != PNGIO::GS)
{
fprintf (stdout, "ERR: Only greyscale images accepted\n");
return -3;
}
fprintf (stdout, "allocating 2x%i bytes for images\n", w*h*sizeof (unsigned char));
fprintf (stdout, "allocating %i bytes for buffer\n", w*h*sizeof (double));
inp = new unsigned char[w*h]; memset (inp, 0, w*h);
img = new double[w*h]; memset (img, 0, w*h*sizeof (double));
fprintf (stdout, "reading image data...\n");
if (PNGIO::readPixelBuffer (app_state.image_filename, inp, w, h, bd, ct) != PNGIO::ERR_NOERR)
{
fprintf (stdout, "ERR: Could not read '%s'\n", app_state.image_filename);
}
else
{
// convert to float
fprintf (stdout, "converting to double...\n");
for (i=0; i<w*h; i++)
{
img[i] = ((double)inp[i])/255.0f;
}
if (app_state.aniso_diffusion)
{
fprintf (stdout, "running anisotropic-diffusion...\n");
nand (img, app_state.iterations, w, h);
fprintf (stdout, "complete\n");
}
// gaussian
if (app_state.iso_diffusion)
{
fprintf (stdout, "running isotropic-diffusion...\n");
gauss (img, app_state.iterations, w, h);
fprintf (stdout, "complete\n");
}
if (app_state.connected_components)
{
fprintf (stdout, "running connected components...\n");
connected_components (inp, w, h);
fprintf (stdout, "complete\n");
// this is just to avoid a branch later... bit ugly
for (i=0; i<w*h; i++)
img[i] = (double)(inp[i])/255.0;
}
}
fprintf (stdout, "saving to '%s'\n", app_state.output_filename);
for (i=0; i<w*h; i++)
inp[i] = (unsigned char)(img[i]*255.0f);
PNGIO::writePixelBuffer (app_state.output_filename, inp, w, h, 8, 1);
// cleanup
fprintf (stdout, "cleanup\n");
delete[] img;
delete[] inp;
fprintf (stdout, "complete\n");
return 0;
}