forked from kevinlin311tw/ObjLeft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnected.cc
77 lines (55 loc) · 1.53 KB
/
connected.cc
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
#include "connected.h"
#define _TEST
#ifdef _TEST
#include <functional>
#include <stdlib.h>
#include <stdio.h>
void test_inline_img()
{
const char *_img = {
" "
" * 0 * "
" ** 0 * "
" ******* "
" * "
" * "
" *** "
};
const unsigned char *img = (const unsigned char *)_img;
int width = 14, height = 7;
unsigned char *out_uc = (unsigned char *)malloc(width*height);
ConnectedComponents cc(30);
cc.connected(img, out_uc, width, height,
std::equal_to<unsigned char>(),
false);
for(int r=0; r<height; ++r) {
for(int c=0; c<width; ++c)
putchar('0'+out_uc[r*width+c]);
putchar('\n');
}
free(out_uc);
}
void test_raw_img(const char *infname, const char *outfname,
int width, int height)
{
unsigned char *img = (unsigned char *)malloc(width*height);
FILE *fin = fopen(infname, "r");
fread(img, 1, width*height, fin);
unsigned int *out = (unsigned int *)malloc(sizeof(*out)*width*height);
ConnectedComponents cc(30);
cc.connected(img, out, width, height,
std::equal_to<unsigned char>(),
constant<bool,true>());
unsigned char *out_uc = (unsigned char *)malloc(width*height);
std::copy(out, out+width*height, out_uc);
FILE *fout = fopen(outfname, "w");
fwrite(out_uc, 1, width*height, fout);
fclose(fout); fclose(fin);
free(img); free(out); free(out_uc);
}
// main()
// {
// test_inline_img();
// test_raw_img("img.raw", "out.raw", 321, 241);
// }
#endif // _TEST