forked from GSam/Capture2Text
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Furigana.cpp
371 lines (298 loc) · 10.3 KB
/
Furigana.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
Copyright (C) 2010-2017 Christopher Brochtrup
This file is part of Capture2Text.
Capture2Text is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Capture2Text is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Capture2Text. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QList>
#include <QDebug>
#include "Furigana.h"
#include "PreProcessCommon.h"
//const float Furigana::FURIGANA_MIN_FG_PIX_PER_LINE = 2.0f;
const float Furigana::FURIGANA_MIN_FG_PIX_PER_LINE = 1.0f;
const float Furigana::FURIGANA_MIN_WIDTH = 5.0f;
Furigana::Furigana()
{
}
// Erase the furigana from the provided binary PIX. Works by finding spans of foreground
// text and removing the spans that are too narrow and are likely furigana.
// Use this version for vertical text.
bool Furigana::eraseFuriganaVertical(PIX *pixs, float scaleFactor, int *numTextLines)
{
int minFgPixPerLine = (int)(FURIGANA_MIN_FG_PIX_PER_LINE * scaleFactor);
int minSpanWidth = (int)(FURIGANA_MIN_WIDTH * scaleFactor);
unsigned int x = 0;
int numFgPixelsOnLine = 0;
int goodLine = LEPT_FALSE;
int numGoodLinesInCurSpan = 0;
int totalGoodLines = 0;
unsigned int pixelValue = 0;
FuriganaSpan span(NO_VALUE, NO_VALUE);
QList<FuriganaSpan> spanList;
// Get list of spans that contain fg pixels
for (x = 0; x < pixs->w; x++)
{
numFgPixelsOnLine = 0;
goodLine = LEPT_FALSE;
for (int y = 0; y < (int)pixs->h; y++)
{
int status = pixGetPixel(pixs, x, y, &pixelValue);
if (status != LEPT_OK)
{
return false;
}
// If this is a foreground pixel
if (pixelValue == 1)
{
numFgPixelsOnLine++;
// If this line has already meet the minimum number of fg pixels, stop scanning it
if (numFgPixelsOnLine >= minFgPixPerLine)
{
goodLine = LEPT_TRUE;
break;
}
}
}
// If last line is good, set it bad in order to close the span
if (goodLine && (x == pixs->w - 1))
{
goodLine = LEPT_FALSE;
numGoodLinesInCurSpan++;
}
// If this line has the minimum number of fg pixels
if (goodLine)
{
// Start a new span
if (span.start == NO_VALUE)
{
span.start = x;
}
numGoodLinesInCurSpan++;
}
else // Line doesn't have enough fg pixels to consider as part of a span
{
// If a span has already been started, then end it
if (span.start != NO_VALUE)
{
// If this span isn't too small (needed so that the average isn't skewed)
if (numGoodLinesInCurSpan >= minSpanWidth)
{
span.end = x;
totalGoodLines += numGoodLinesInCurSpan;
// Add span to the list
spanList.append(FuriganaSpan(span.start, span.end));
}
}
// Reset span
span.start = NO_VALUE;
span.end = NO_VALUE;
numGoodLinesInCurSpan = 0;
}
}
if (spanList.size() == 0)
{
return true;
}
// Get mean width of the largest 50% of spans
QList<double> spanLengths;
for(auto span : spanList)
{
spanLengths.append(span.getLength());
}
qSort(spanLengths);
double meanUpperHalf = 0.0;
for(int i = spanLengths.size() / 2; i < spanLengths.size(); i++)
{
meanUpperHalf += spanLengths[i];
}
meanUpperHalf /= (spanLengths.size() - spanLengths.size() / 2);
x = 0;
int numMinorSpans = 0;
// Erase areas of the PIX where either no span exists or where a span is too narrow
for (int spanIdx = 0; spanIdx < spanList.size(); spanIdx++)
{
span = spanList.at(spanIdx);
// If span is at least X% of average width, erase area between the previous span and this span
if (span.getLength() >= (int)(meanUpperHalf * 0.6))
{
bool status = eraseAreaLeftToRight(pixs, x, span.start - x);
if (!status)
{
return false;
}
x = span.end + 1;
}
else
{
numMinorSpans++;
}
}
*numTextLines = qMax(spanList.size() - numMinorSpans, 1);
// Clear area between the end of the right-most span and the right edge of the PIX
if ((x != 0) && (x < (pixs->w - 1)))
{
bool status = eraseAreaLeftToRight(pixs, x, pixs->w - x);
if (!status)
{
return false;
}
}
return true;
}
// Erase the furigana from the provided binary PIX. Works by finding spans of foreground
// text and removing the spans that are too narrow and are likely furigana.
// Use this version for horizontal text.
bool Furigana::eraseFuriganaHorizontal(PIX *pixs, float scaleFactor, int *numTextLines)
{
int minFgPixPerLine = (int)(FURIGANA_MIN_FG_PIX_PER_LINE * scaleFactor);
int minSpanWidth = (int)(FURIGANA_MIN_WIDTH * scaleFactor);
unsigned int y = 0;
int numFgPixelsOnLine = 0;
int goodLine = LEPT_FALSE;
int numGoodLinesInCurSpan = 0;
int totalGoodLines = 0;
unsigned int pixelValue = 0;
FuriganaSpan span(NO_VALUE, NO_VALUE);
QList<FuriganaSpan> spanList;
// Get list of spans that contain fg pixels
for (y = 0; y < pixs->h; y++)
{
numFgPixelsOnLine = 0;
goodLine = LEPT_FALSE;
for (int x = 0; x < (int)pixs->w; x++)
{
int status = pixGetPixel(pixs, x, y, &pixelValue);
if (status != LEPT_OK)
{
return false;
}
// If this is a foreground pixel
if (pixelValue == 1)
{
numFgPixelsOnLine++;
// If this line has already meet the minimum number of fg pixels, stop scanning it
if (numFgPixelsOnLine >= minFgPixPerLine)
{
goodLine = LEPT_TRUE;
break;
}
}
}
// If last line is good, set it bad in order to close the span
if (goodLine && (y == pixs->h - 1))
{
goodLine = LEPT_FALSE;
numGoodLinesInCurSpan++;
}
// If this line has the minimum number of fg pixels
if (goodLine)
{
// Start a new span
if (span.start == NO_VALUE)
{
span.start = y;
}
numGoodLinesInCurSpan++;
}
else // Line doesn't have enough fg pixels to consider as part of a span
{
// If a span has already been started, then end it
if (span.start != NO_VALUE)
{
// If this span isn't too small (needed so that the average isn't skewed)
if (numGoodLinesInCurSpan >= minSpanWidth)
{
span.end = y;
totalGoodLines += numGoodLinesInCurSpan;
// Add span to the list
spanList.append(FuriganaSpan(span.start, span.end));
}
}
// Reset span
span.start = NO_VALUE;
span.end = NO_VALUE;
numGoodLinesInCurSpan = 0;
}
}
if (spanList.size() == 0)
{
return true;
}
// Get mean width of the largest 50% of spans
QList<double> spanLengths;
for(auto span : spanList)
{
spanLengths.append(span.getLength());
}
qSort(spanLengths);
double meanUpperHalf = 0.0;
for(int i = spanLengths.size() / 2; i < spanLengths.size(); i++)
{
meanUpperHalf += spanLengths[i];
}
meanUpperHalf /= (spanLengths.size() - spanLengths.size() / 2);
y = 0;
int numMinorSpans = 0;
// Erase areas of the PIX where either no span exists or where a span is too narrow
for (int spanIdx = 0; spanIdx < spanList.size(); spanIdx++)
{
span = spanList.at(spanIdx);
// If span is at least X% of average width, erase area between the previous span and this span
if (span.getLength() >= (int)(meanUpperHalf * 0.6))
{
bool status = eraseAreaTopToBottom(pixs, y, span.start - y);
if (!status)
{
return false;
}
y = span.end + 1;
}
else
{
numMinorSpans++;
}
}
*numTextLines = qMax(spanList.size() - numMinorSpans, 1);
// Clear area between the end of the right-most span and the right edge of the PIX
if ((y != 0) && (y < (pixs->h - 1)))
{
bool status = eraseAreaTopToBottom(pixs, y, pixs->h - y);
if (!status)
{
return false;
}
}
return true;
}
// Clear/erase a left-to-right section of the provided binary PIX.
bool Furigana::eraseAreaLeftToRight(PIX *pixs, int x, int width)
{
int status = LEPT_ERROR;
BOX box;
box.x = x;
box.y = 0;
box.w = width;
box.h = pixs->h;
status = pixClearInRect(pixs, &box);
return (status == LEPT_OK);
}
// Clear/erase a top-to-bottom section of the provided binary PIX.
bool Furigana::eraseAreaTopToBottom(PIX *pixs, int y, int height)
{
int status = LEPT_ERROR;
BOX box;
box.x = 0;
box.y = y;
box.w = pixs->w;
box.h = height;
status = pixClearInRect(pixs, &box);
return (status == LEPT_OK) ;
}