/
line_splitter.cpp
65 lines (59 loc) · 1.49 KB
/
line_splitter.cpp
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
#include "line_splitter.hpp"
namespace LineSplitter
{
QList<QImage> split(const QImage& img)
{
QVector<bool> emptyLines(img.height(), true);
for (int y = 0; y < img.height(); ++y) {
const uchar* it = img.constScanLine(y);
const uchar* end = it + img.width();
while (it < end) {
if (*it++ > 10) {
emptyLines[y] = false;
break;
}
}
}
QList<QImage> lines;
for (int y = 0; y < img.height();) {
if (emptyLines[y]) {
++y;
continue;
}
QRect r(0, y, img.width(), 1);
int height = 0;
for (; y < img.height() && !emptyLines[y]; ++y)
++height;
r.setHeight(height);
if (height > 60 && height < 80) {
lines += splitPath(img.copy(r));
} else {
lines << img.copy(r);
}
}
return lines;
}
QList<QImage> splitPath(const QImage& img)
{
QImage up = img;
QImage down = img;
for (int x = 0; x < img.width(); ++x) {
bool found = false;
for (int i = 1; i < 10; ++i) {
int y = img.height() / 2 + i / 2 * ((i%2) ? 1 : -1);
if (img.pixelIndex(x, y) <= 127) {
for (int z = 0; z < y; ++z)
down.setPixel(x, z, 0);
for (int z = y+1; z < up.height(); ++z)
up.setPixel(x, z, 0);
found = true;
break;
}
}
if (!found) {
return QList<QImage>() << img;
}
}
return QList<QImage>() << split(up) << split(down);
}
}