Coverage Report

Created: 2026-01-13 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tesseract/src/textord/tordmain.cpp
Line
Count
Source
1
/**********************************************************************
2
 * File:        tordmain.cpp  (Formerly textordp.c)
3
 * Description: C++ top level textord code.
4
 * Author:      Ray Smith
5
 *
6
 * (C) Copyright 1992, Hewlett-Packard Ltd.
7
 ** Licensed under the Apache License, Version 2.0 (the "License");
8
 ** you may not use this file except in compliance with the License.
9
 ** You may obtain a copy of the License at
10
 ** http://www.apache.org/licenses/LICENSE-2.0
11
 ** Unless required by applicable law or agreed to in writing, software
12
 ** distributed under the License is distributed on an "AS IS" BASIS,
13
 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 ** See the License for the specific language governing permissions and
15
 ** limitations under the License.
16
 *
17
 **********************************************************************/
18
19
#define _USE_MATH_DEFINES // for M_PI
20
21
#ifdef HAVE_CONFIG_H
22
#  include "config_auto.h"
23
#endif
24
25
#include "tordmain.h"
26
27
#include "arrayaccess.h" // for GET_DATA_BYTE
28
#include "blobbox.h"     // for BLOBNBOX_IT, BLOBNBOX, TO_BLOCK, TO_B...
29
#include "ccstruct.h"    // for CCStruct, CCStruct::kXHeightFraction
30
#include "clst.h"        // for CLISTIZE
31
#include "coutln.h"      // for C_OUTLINE_IT, C_OUTLINE_LIST, C_OUTLINE
32
#include "drawtord.h"    // for plot_box_list, to_win, create_to_win
33
#include "edgblob.h"     // for extract_edges
34
#include "errcode.h"     // for ASSERT_HOST, ...
35
#include "makerow.h"     // for textord_test_x, textord_test_y, texto...
36
#include "ocrblock.h"    // for BLOCK_IT, BLOCK, BLOCK_LIST (ptr only)
37
#include "ocrrow.h"      // for ROW, ROW_IT, ROW_LIST, tweak_row_base...
38
#include "params.h"      // for DoubleParam, BoolParam, IntParam
39
#include "pdblock.h"     // for PDBLK
40
#include "points.h"      // for FCOORD, ICOORD
41
#include "polyblk.h"     // for POLY_BLOCK
42
#include "quadratc.h"    // for QUAD_COEFFS
43
#include "quspline.h"    // for QSPLINE, tweak_row_baseline
44
#include "rect.h"        // for TBOX
45
#include "scrollview.h"  // for ScrollView, ScrollView::WHITE
46
#include "statistc.h"    // for STATS
47
#include "stepblob.h"    // for C_BLOB_IT, C_BLOB, C_BLOB_LIST
48
#include "textord.h"     // for Textord, WordWithBox, WordGrid, WordS...
49
#include "tprintf.h"     // for tprintf
50
#include "werd.h"        // for WERD_IT, WERD, WERD_LIST, W_DONT_CHOP
51
52
#include <allheaders.h> // for pixDestroy, pixGetHeight, boxCreate
53
54
#include <cfloat>  // for FLT_MAX
55
#include <cmath>   // for ceil, floor, M_PI
56
#include <cstdint> // for INT16_MAX, uint32_t, int32_t, int16_t
57
#include <memory>
58
59
namespace tesseract {
60
61
16.2k
#define MAX_NEAREST_DIST 600 // for block skew stats
62
63
/**********************************************************************
64
 * SetBlobStrokeWidth
65
 *
66
 * Set the horizontal and vertical stroke widths in the blob.
67
 **********************************************************************/
68
2.79M
void SetBlobStrokeWidth(Image pix, BLOBNBOX *blob) {
69
  // Cut the blob rectangle into a Pix.
70
2.79M
  int pix_height = pixGetHeight(pix);
71
2.79M
  const TBOX &box = blob->bounding_box();
72
2.79M
  int width = box.width();
73
2.79M
  int height = box.height();
74
2.79M
  Box *blob_pix_box = boxCreate(box.left(), pix_height - box.top(), width, height);
75
2.79M
  Image pix_blob = pixClipRectangle(pix, blob_pix_box, nullptr);
76
2.79M
  boxDestroy(&blob_pix_box);
77
2.79M
  Image dist_pix = pixDistanceFunction(pix_blob, 4, 8, L_BOUNDARY_BG);
78
2.79M
  pix_blob.destroy();
79
  // Compute the stroke widths.
80
2.79M
  uint32_t *data = pixGetData(dist_pix);
81
2.79M
  int wpl = pixGetWpl(dist_pix);
82
  // Horizontal width of stroke.
83
2.79M
  STATS h_stats(0, width);
84
20.4M
  for (int y = 0; y < height; ++y) {
85
17.6M
    uint32_t *pixels = data + y * wpl;
86
17.6M
    int prev_pixel = 0;
87
17.6M
    int pixel = GET_DATA_BYTE(pixels, 0);
88
150M
    for (int x = 1; x < width; ++x) {
89
132M
      int next_pixel = GET_DATA_BYTE(pixels, x);
90
      // We are looking for a pixel that is equal to its vertical neighbours,
91
      // yet greater than its left neighbour.
92
132M
      if (prev_pixel < pixel && (y == 0 || pixel == GET_DATA_BYTE(pixels - wpl, x - 1)) &&
93
19.1M
          (y == height - 1 || pixel == GET_DATA_BYTE(pixels + wpl, x - 1))) {
94
12.5M
        if (pixel > next_pixel) {
95
          // Single local max, so an odd width.
96
6.81M
          h_stats.add(pixel * 2 - 1, 1);
97
6.81M
        } else if (pixel == next_pixel && x + 1 < width && pixel > GET_DATA_BYTE(pixels, x + 1)) {
98
          // Double local max, so an even width.
99
2.91M
          h_stats.add(pixel * 2, 1);
100
2.91M
        }
101
12.5M
      }
102
132M
      prev_pixel = pixel;
103
132M
      pixel = next_pixel;
104
132M
    }
105
17.6M
  }
106
  // Vertical width of stroke.
107
2.79M
  STATS v_stats(0, height);
108
15.9M
  for (int x = 0; x < width; ++x) {
109
13.1M
    int prev_pixel = 0;
110
13.1M
    int pixel = GET_DATA_BYTE(data, x);
111
150M
    for (int y = 1; y < height; ++y) {
112
136M
      uint32_t *pixels = data + y * wpl;
113
136M
      int next_pixel = GET_DATA_BYTE(pixels, x);
114
      // We are looking for a pixel that is equal to its horizontal neighbours,
115
      // yet greater than its upper neighbour.
116
136M
      if (prev_pixel < pixel && (x == 0 || pixel == GET_DATA_BYTE(pixels - wpl, x - 1)) &&
117
19.5M
          (x == width - 1 || pixel == GET_DATA_BYTE(pixels - wpl, x + 1))) {
118
13.5M
        if (pixel > next_pixel) {
119
          // Single local max, so an odd width.
120
6.42M
          v_stats.add(pixel * 2 - 1, 1);
121
7.08M
        } else if (pixel == next_pixel && y + 1 < height &&
122
4.39M
                   pixel > GET_DATA_BYTE(pixels + wpl, x)) {
123
          // Double local max, so an even width.
124
2.69M
          v_stats.add(pixel * 2, 1);
125
2.69M
        }
126
13.5M
      }
127
136M
      prev_pixel = pixel;
128
136M
      pixel = next_pixel;
129
136M
    }
130
13.1M
  }
131
2.79M
  dist_pix.destroy();
132
  // Store the horizontal and vertical width in the blob, keeping both
133
  // widths if there is enough information, otherwise only the one with
134
  // the most samples.
135
  // If there are insufficient samples, store zero, rather than using
136
  // 2*area/perimeter, as the numbers that gives do not match the numbers
137
  // from the distance method.
138
2.79M
  if (h_stats.get_total() >= (width + height) / 4) {
139
688k
    blob->set_horz_stroke_width(h_stats.ile(0.5f));
140
688k
    if (v_stats.get_total() >= (width + height) / 4) {
141
183k
      blob->set_vert_stroke_width(v_stats.ile(0.5f));
142
504k
    } else {
143
504k
      blob->set_vert_stroke_width(0.0f);
144
504k
    }
145
2.10M
  } else {
146
2.10M
    if (v_stats.get_total() >= (width + height) / 4 || v_stats.get_total() > h_stats.get_total()) {
147
327k
      blob->set_horz_stroke_width(0.0f);
148
327k
      blob->set_vert_stroke_width(v_stats.ile(0.5f));
149
1.78M
    } else {
150
1.78M
      blob->set_horz_stroke_width(h_stats.get_total() > 2 ? h_stats.ile(0.5f) : 0.0f);
151
1.78M
      blob->set_vert_stroke_width(0.0f);
152
1.78M
    }
153
2.10M
  }
154
2.79M
}
155
156
/**********************************************************************
157
 * assign_blobs_to_blocks2
158
 *
159
 * Make a list of TO_BLOCKs for portrait and landscape orientation.
160
 **********************************************************************/
161
162
void assign_blobs_to_blocks2(Image pix,
163
                             BLOCK_LIST *blocks,           // blocks to process
164
16.2k
                             TO_BLOCK_LIST *port_blocks) { // output list
165
16.2k
  BLOCK_IT block_it = blocks;
166
16.2k
  C_BLOB_IT blob_it;       // iterator
167
16.2k
  BLOBNBOX_IT port_box_it; // iterator
168
                           // destination iterator
169
16.2k
  TO_BLOCK_IT port_block_it = port_blocks;
170
171
32.4k
  for (block_it.mark_cycle_pt(); !block_it.cycled_list(); block_it.forward()) {
172
16.2k
    auto block = block_it.data();
173
16.2k
    auto port_block = new TO_BLOCK(block);
174
175
    // Convert the good outlines to block->blob_list
176
16.2k
    port_box_it.set_to_list(&port_block->blobs);
177
16.2k
    blob_it.set_to_list(block->blob_list());
178
2.80M
    for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
179
2.79M
      auto blob = blob_it.extract();
180
2.79M
      auto newblob = new BLOBNBOX(blob); // Convert blob to BLOBNBOX.
181
2.79M
      newblob->set_owns_cblob(true);
182
2.79M
      SetBlobStrokeWidth(pix, newblob);
183
2.79M
      port_box_it.add_after_then_move(newblob);
184
2.79M
    }
185
186
    // Put the rejected outlines in block->noise_blobs, which allows them to
187
    // be reconsidered and sorted back into rows and recover outlines mistakenly
188
    // rejected.
189
16.2k
    port_box_it.set_to_list(&port_block->noise_blobs);
190
16.2k
    blob_it.set_to_list(block->reject_blobs());
191
19.8k
    for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
192
3.59k
      auto blob = blob_it.extract();
193
3.59k
      auto newblob = new BLOBNBOX(blob); // Convert blob to BLOBNBOX.
194
3.59k
      newblob->set_owns_cblob(true);
195
3.59k
      SetBlobStrokeWidth(pix, newblob);
196
3.59k
      port_box_it.add_after_then_move(newblob);
197
3.59k
    }
198
199
16.2k
    port_block_it.add_after_then_move(port_block);
200
16.2k
  }
201
16.2k
}
202
203
/**********************************************************************
204
 * find_components
205
 *
206
 * Find the C_OUTLINEs of the connected components in each block, put them
207
 * in C_BLOBs, and filter them by size, putting the different size
208
 * grades on different lists in the matching TO_BLOCK in to_blocks.
209
 **********************************************************************/
210
211
16.2k
void Textord::find_components(Image pix, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks) {
212
16.2k
  int width = pixGetWidth(pix);
213
16.2k
  int height = pixGetHeight(pix);
214
16.2k
  if (width > INT16_MAX || height > INT16_MAX) {
215
0
    tprintf("Input image too large! (%d, %d)\n", width, height);
216
0
    return; // Can't handle it.
217
0
  }
218
219
16.2k
  BLOCK_IT block_it(blocks); // iterator
220
32.4k
  for (block_it.mark_cycle_pt(); !block_it.cycled_list(); block_it.forward()) {
221
16.2k
    BLOCK *block = block_it.data();
222
16.2k
    if (block->pdblk.poly_block() == nullptr || block->pdblk.poly_block()->IsText()) {
223
16.2k
      extract_edges(pix, block);
224
16.2k
    }
225
16.2k
  }
226
227
16.2k
  assign_blobs_to_blocks2(pix, blocks, to_blocks);
228
16.2k
  ICOORD page_tr(width, height);
229
16.2k
  filter_blobs(page_tr, to_blocks, !textord_test_landscape);
230
16.2k
}
231
232
/**********************************************************************
233
 * filter_blobs
234
 *
235
 * Sort the blobs into sizes in all the blocks for later work.
236
 **********************************************************************/
237
238
void Textord::filter_blobs(ICOORD page_tr,        // top right
239
                           TO_BLOCK_LIST *blocks, // output list
240
16.2k
                           bool testing_on) {     // for plotting
241
16.2k
  TO_BLOCK_IT block_it = blocks;                  // destination iterator
242
16.2k
  TO_BLOCK *block;                                // created block
243
244
#ifndef GRAPHICS_DISABLED
245
  if (to_win != nullptr) {
246
    to_win->Clear();
247
  }
248
#endif // !GRAPHICS_DISABLED
249
250
32.4k
  for (block_it.mark_cycle_pt(); !block_it.cycled_list(); block_it.forward()) {
251
16.2k
    block = block_it.data();
252
16.2k
    block->line_size = filter_noise_blobs(&block->blobs, &block->noise_blobs, &block->small_blobs,
253
16.2k
                                          &block->large_blobs);
254
16.2k
    if (block->line_size == 0) {
255
496
      block->line_size = 1;
256
496
    }
257
16.2k
    block->line_spacing =
258
16.2k
        block->line_size *
259
16.2k
        (tesseract::CCStruct::kDescenderFraction + tesseract::CCStruct::kXHeightFraction +
260
16.2k
         2 * tesseract::CCStruct::kAscenderFraction) /
261
16.2k
        tesseract::CCStruct::kXHeightFraction;
262
16.2k
    block->line_size *= textord_min_linesize;
263
16.2k
    block->max_blob_size = block->line_size * textord_excess_blobsize;
264
265
#ifndef GRAPHICS_DISABLED
266
    if (textord_show_blobs && testing_on) {
267
      if (to_win == nullptr) {
268
        create_to_win(page_tr);
269
      }
270
      block->plot_graded_blobs(to_win);
271
    }
272
    if (textord_show_boxes && testing_on) {
273
      if (to_win == nullptr) {
274
        create_to_win(page_tr);
275
      }
276
      plot_box_list(to_win, &block->noise_blobs, ScrollView::WHITE);
277
      plot_box_list(to_win, &block->small_blobs, ScrollView::WHITE);
278
      plot_box_list(to_win, &block->large_blobs, ScrollView::WHITE);
279
      plot_box_list(to_win, &block->blobs, ScrollView::WHITE);
280
    }
281
#endif // !GRAPHICS_DISABLED
282
16.2k
  }
283
16.2k
}
284
285
/**********************************************************************
286
 * filter_noise_blobs
287
 *
288
 * Move small blobs to a separate list.
289
 **********************************************************************/
290
291
float Textord::filter_noise_blobs(BLOBNBOX_LIST *src_list,     // original list
292
                                  BLOBNBOX_LIST *noise_list,   // noise list
293
                                  BLOBNBOX_LIST *small_list,   // small blobs
294
16.2k
                                  BLOBNBOX_LIST *large_list) { // large blobs
295
16.2k
  int16_t height;                                              // height of blob
296
16.2k
  int16_t width;                                               // of blob
297
16.2k
  BLOBNBOX *blob;                                              // current blob
298
16.2k
  float initial_x;                                             // first guess
299
16.2k
  BLOBNBOX_IT src_it = src_list;                               // iterators
300
16.2k
  BLOBNBOX_IT noise_it = noise_list;
301
16.2k
  BLOBNBOX_IT small_it = small_list;
302
16.2k
  BLOBNBOX_IT large_it = large_list;
303
16.2k
  STATS size_stats(0, MAX_NEAREST_DIST - 1);
304
  // blob heights
305
16.2k
  float min_y; // size limits
306
16.2k
  float max_y;
307
16.2k
  float max_x;
308
16.2k
  float max_height; // of good blobs
309
310
2.80M
  for (src_it.mark_cycle_pt(); !src_it.cycled_list(); src_it.forward()) {
311
2.79M
    blob = src_it.data();
312
2.79M
    if (blob->bounding_box().height() < textord_max_noise_size) {
313
1.66M
      noise_it.add_after_then_move(src_it.extract());
314
1.66M
    } else if (blob->enclosed_area() >= blob->bounding_box().height() *
315
1.12M
                                            blob->bounding_box().width() *
316
1.12M
                                            textord_noise_area_ratio) {
317
540k
      small_it.add_after_then_move(src_it.extract());
318
540k
    }
319
2.79M
  }
320
602k
  for (src_it.mark_cycle_pt(); !src_it.cycled_list(); src_it.forward()) {
321
586k
    size_stats.add(src_it.data()->bounding_box().height(), 1);
322
586k
  }
323
16.2k
  initial_x = size_stats.ile(textord_initialx_ile);
324
16.2k
  max_y = ceil(initial_x *
325
16.2k
               (tesseract::CCStruct::kDescenderFraction + tesseract::CCStruct::kXHeightFraction +
326
16.2k
                2 * tesseract::CCStruct::kAscenderFraction) /
327
16.2k
               tesseract::CCStruct::kXHeightFraction);
328
16.2k
  min_y = std::floor(initial_x / 2);
329
16.2k
  max_x = ceil(initial_x * textord_width_limit);
330
16.2k
  small_it.move_to_first();
331
557k
  for (small_it.mark_cycle_pt(); !small_it.cycled_list(); small_it.forward()) {
332
540k
    height = small_it.data()->bounding_box().height();
333
540k
    if (height > max_y) {
334
3.72k
      large_it.add_after_then_move(small_it.extract());
335
537k
    } else if (height >= min_y) {
336
512k
      src_it.add_after_then_move(small_it.extract());
337
512k
    }
338
540k
  }
339
16.2k
  size_stats.clear();
340
1.11M
  for (src_it.mark_cycle_pt(); !src_it.cycled_list(); src_it.forward()) {
341
1.09M
    height = src_it.data()->bounding_box().height();
342
1.09M
    width = src_it.data()->bounding_box().width();
343
1.09M
    if (height < min_y) {
344
23.1k
      small_it.add_after_then_move(src_it.extract());
345
1.07M
    } else if (height > max_y || width > max_x) {
346
16.2k
      large_it.add_after_then_move(src_it.extract());
347
1.05M
    } else {
348
1.05M
      size_stats.add(height, 1);
349
1.05M
    }
350
1.09M
  }
351
16.2k
  max_height = size_stats.ile(textord_initialasc_ile);
352
  //      tprintf("max_y=%g, min_y=%g, initial_x=%g, max_height=%g,",
353
  //              max_y,min_y,initial_x,max_height);
354
16.2k
  max_height *= tesseract::CCStruct::kXHeightCapRatio;
355
16.2k
  if (max_height > initial_x) {
356
2.43k
    initial_x = max_height;
357
2.43k
  }
358
  //      tprintf(" ret=%g\n",initial_x);
359
16.2k
  return initial_x;
360
16.2k
}
361
362
// Fixes the block so it obeys all the rules:
363
// Must have at least one ROW.
364
// Must have at least one WERD.
365
// WERDs contain a fake blob.
366
0
void Textord::cleanup_nontext_block(BLOCK *block) {
367
  // Non-text blocks must contain at least one row.
368
0
  ROW_IT row_it(block->row_list());
369
0
  if (row_it.empty()) {
370
0
    const TBOX &box = block->pdblk.bounding_box();
371
0
    float height = box.height();
372
0
    int32_t xstarts[2] = {box.left(), box.right()};
373
0
    double coeffs[3] = {0.0, 0.0, static_cast<double>(box.bottom())};
374
0
    ROW *row = new ROW(1, xstarts, coeffs, height / 2.0f, height / 4.0f, height / 4.0f, 0, 1);
375
0
    row_it.add_after_then_move(row);
376
0
  }
377
  // Each row must contain at least one word.
378
0
  for (row_it.mark_cycle_pt(); !row_it.cycled_list(); row_it.forward()) {
379
0
    ROW *row = row_it.data();
380
0
    WERD_IT w_it(row->word_list());
381
0
    if (w_it.empty()) {
382
      // Make a fake blob to put in the word.
383
0
      TBOX box = block->row_list()->singleton() ? block->pdblk.bounding_box() : row->bounding_box();
384
0
      C_BLOB *blob = C_BLOB::FakeBlob(box);
385
0
      C_BLOB_LIST blobs;
386
0
      C_BLOB_IT blob_it(&blobs);
387
0
      blob_it.add_after_then_move(blob);
388
0
      WERD *word = new WERD(&blobs, 0, nullptr);
389
0
      w_it.add_after_then_move(word);
390
0
    }
391
    // Each word must contain a fake blob.
392
0
    for (w_it.mark_cycle_pt(); !w_it.cycled_list(); w_it.forward()) {
393
0
      WERD *word = w_it.data();
394
      // Just assert that this is true, as it would be useful to find
395
      // out why it isn't.
396
0
      ASSERT_HOST(!word->cblob_list()->empty());
397
0
    }
398
0
    row->recalc_bounding_box();
399
0
  }
400
0
}
401
402
/**********************************************************************
403
 * cleanup_blocks
404
 *
405
 * Delete empty blocks, rows from the page.
406
 **********************************************************************/
407
408
16.2k
void Textord::cleanup_blocks(bool clean_noise, BLOCK_LIST *blocks) {
409
16.2k
  BLOCK_IT block_it = blocks; // iterator
410
16.2k
  ROW_IT row_it;              // row iterator
411
412
16.2k
  int num_rows = 0;
413
16.2k
  int num_rows_all = 0;
414
16.2k
  int num_blocks = 0;
415
16.2k
  int num_blocks_all = 0;
416
32.4k
  for (block_it.mark_cycle_pt(); !block_it.cycled_list(); block_it.forward()) {
417
16.2k
    BLOCK *block = block_it.data();
418
16.2k
    if (block->pdblk.poly_block() != nullptr && !block->pdblk.poly_block()->IsText()) {
419
0
      cleanup_nontext_block(block);
420
0
      continue;
421
0
    }
422
16.2k
    num_rows = 0;
423
16.2k
    num_rows_all = 0;
424
16.2k
    if (clean_noise) {
425
16.2k
      row_it.set_to_list(block->row_list());
426
194k
      for (row_it.mark_cycle_pt(); !row_it.cycled_list(); row_it.forward()) {
427
178k
        ROW *row = row_it.data();
428
178k
        ++num_rows_all;
429
178k
        clean_small_noise_from_words(row);
430
178k
        if ((textord_noise_rejrows && !row->word_list()->empty() && clean_noise_from_row(row)) ||
431
146k
            row->word_list()->empty()) {
432
37.6k
          delete row_it.extract(); // lose empty row.
433
140k
        } else {
434
140k
          if (textord_noise_rejwords) {
435
140k
            clean_noise_from_words(row_it.data());
436
140k
          }
437
140k
          if (textord_blshift_maxshift >= 0) {
438
140k
            tweak_row_baseline(row, textord_blshift_maxshift, textord_blshift_xfraction);
439
140k
          }
440
140k
          ++num_rows;
441
140k
        }
442
178k
      }
443
16.2k
    }
444
16.2k
    if (block->row_list()->empty()) {
445
1.76k
      delete block_it.extract(); // Lose empty text blocks.
446
14.4k
    } else {
447
14.4k
      ++num_blocks;
448
14.4k
    }
449
16.2k
    ++num_blocks_all;
450
16.2k
    if (textord_noise_debug) {
451
0
      tprintf("cleanup_blocks: # rows = %d / %d\n", num_rows, num_rows_all);
452
0
    }
453
16.2k
  }
454
16.2k
  if (textord_noise_debug) {
455
0
    tprintf("cleanup_blocks: # blocks = %d / %d\n", num_blocks, num_blocks_all);
456
0
  }
457
16.2k
}
458
459
/**********************************************************************
460
 * clean_noise_from_row
461
 *
462
 * Move blobs of words from rows of garbage into the reject blobs list.
463
 **********************************************************************/
464
465
bool Textord::clean_noise_from_row( // remove empties
466
    ROW *row                        // row to clean
467
173k
) {
468
173k
  bool testing_on;
469
173k
  TBOX blob_box;            // bounding box
470
173k
  C_BLOB *blob;             // current blob
471
173k
  C_OUTLINE *outline;       // current outline
472
173k
  WERD *word;               // current word
473
173k
  int32_t blob_size;        // biggest size
474
173k
  int32_t trans_count = 0;  // no of transitions
475
173k
  int32_t trans_threshold;  // noise tolerance
476
173k
  int32_t dot_count;        // small objects
477
173k
  int32_t norm_count;       // normal objects
478
173k
  int32_t super_norm_count; // real char-like
479
                            // words of row
480
173k
  WERD_IT word_it = row->word_list();
481
173k
  C_BLOB_IT blob_it;   // blob iterator
482
173k
  C_OUTLINE_IT out_it; // outline iterator
483
484
173k
  testing_on = textord_test_y > row->base_line(textord_test_x) && textord_show_blobs &&
485
0
               textord_test_y < row->base_line(textord_test_x) + row->x_height();
486
173k
  dot_count = 0;
487
173k
  norm_count = 0;
488
173k
  super_norm_count = 0;
489
456k
  for (word_it.mark_cycle_pt(); !word_it.cycled_list(); word_it.forward()) {
490
283k
    word = word_it.data(); // current word
491
                           // blobs in word
492
283k
    blob_it.set_to_list(word->cblob_list());
493
1.39M
    for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
494
1.10M
      blob = blob_it.data();
495
1.10M
      if (!word->flag(W_DONT_CHOP)) {
496
        // get outlines
497
1.07M
        out_it.set_to_list(blob->out_list());
498
3.03M
        for (out_it.mark_cycle_pt(); !out_it.cycled_list(); out_it.forward()) {
499
1.96M
          outline = out_it.data();
500
1.96M
          blob_box = outline->bounding_box();
501
1.96M
          blob_size = blob_box.width() > blob_box.height() ? blob_box.width() : blob_box.height();
502
1.96M
          if (blob_size < textord_noise_sizelimit * row->x_height()) {
503
1.20M
            dot_count++; // count small outlines
504
1.20M
          }
505
1.96M
          if (!outline->child()->empty() &&
506
64.0k
              blob_box.height() < (1 + textord_noise_syfract) * row->x_height() &&
507
54.6k
              blob_box.height() > (1 - textord_noise_syfract) * row->x_height() &&
508
15.6k
              blob_box.width() < (1 + textord_noise_sxfract) * row->x_height() &&
509
12.8k
              blob_box.width() > (1 - textord_noise_sxfract) * row->x_height()) {
510
4.35k
            super_norm_count++; // count small outlines
511
4.35k
          }
512
1.96M
        }
513
1.07M
      } else {
514
36.6k
        super_norm_count++;
515
36.6k
      }
516
1.10M
      blob_box = blob->bounding_box();
517
1.10M
      blob_size = blob_box.width() > blob_box.height() ? blob_box.width() : blob_box.height();
518
1.10M
      if (blob_size >= textord_noise_sizelimit * row->x_height() &&
519
785k
          blob_size < row->x_height() * 2) {
520
739k
        trans_threshold = blob_size / textord_noise_sizefraction;
521
739k
        trans_count = blob->count_transitions(trans_threshold);
522
739k
        if (trans_count < textord_noise_translimit) {
523
665k
          norm_count++;
524
665k
        }
525
739k
      } else if (blob_box.height() > row->x_height() * 2 &&
526
21.1k
                 (!word_it.at_first() || !blob_it.at_first())) {
527
14.2k
        dot_count += 2;
528
14.2k
      }
529
1.10M
      if (testing_on) {
530
0
        tprintf("Blob at (%d,%d) -> (%d,%d), ols=%d, tc=%d, bldiff=%g\n", blob_box.left(),
531
0
                blob_box.bottom(), blob_box.right(), blob_box.top(), blob->out_list()->length(),
532
0
                trans_count, blob_box.bottom() - row->base_line(blob_box.left()));
533
0
      }
534
1.10M
    }
535
283k
  }
536
  // TODO: check whether `&& super_norm_count < textord_noise_sncount`should always be added here.
537
173k
  bool rejected = dot_count > norm_count * textord_noise_normratio &&
538
44.5k
                  dot_count > 2;
539
173k
  if (textord_noise_debug) {
540
0
    tprintf("Row ending at (%d,%g):", blob_box.right(), row->base_line(blob_box.right()));
541
0
    tprintf(" R=%g, dc=%d, nc=%d, %s\n",
542
0
            norm_count > 0 ? static_cast<float>(dot_count) / norm_count : 9999, dot_count,
543
0
            norm_count,
544
0
            rejected? "REJECTED": "ACCEPTED");
545
0
  }
546
173k
  return super_norm_count < textord_noise_sncount && rejected;
547
173k
}
548
549
/**********************************************************************
550
 * clean_noise_from_words
551
 *
552
 * Move blobs of words from rows of garbage into the reject blobs list.
553
 **********************************************************************/
554
555
void Textord::clean_noise_from_words( // remove empties
556
    ROW *row                          // row to clean
557
140k
) {
558
140k
  TBOX blob_box;           // bounding box
559
140k
  C_BLOB *blob;            // current blob
560
140k
  C_OUTLINE *outline;      // current outline
561
140k
  WERD *word;              // current word
562
140k
  int32_t blob_size;       // biggest size
563
140k
  int32_t trans_count;     // no of transitions
564
140k
  int32_t trans_threshold; // noise tolerance
565
140k
  int32_t dot_count;       // small objects
566
140k
  int32_t norm_count;      // normal objects
567
140k
  int32_t dud_words;       // number discarded
568
140k
  int32_t ok_words;        // number remaining
569
140k
  int32_t word_index;      // current word
570
                           // words of row
571
140k
  WERD_IT word_it = row->word_list();
572
140k
  C_BLOB_IT blob_it;   // blob iterator
573
140k
  C_OUTLINE_IT out_it; // outline iterator
574
575
140k
  ok_words = word_it.length();
576
140k
  if (ok_words == 0 || textord_no_rejects) {
577
0
    return;
578
0
  }
579
  // was it chucked
580
140k
  std::vector<int8_t> word_dud(ok_words);
581
140k
  dud_words = 0;
582
140k
  ok_words = 0;
583
140k
  word_index = 0;
584
346k
  for (word_it.mark_cycle_pt(); !word_it.cycled_list(); word_it.forward()) {
585
205k
    word = word_it.data(); // current word
586
205k
    dot_count = 0;
587
205k
    norm_count = 0;
588
    // blobs in word
589
205k
    blob_it.set_to_list(word->cblob_list());
590
907k
    for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
591
701k
      blob = blob_it.data();
592
701k
      if (!word->flag(W_DONT_CHOP)) {
593
        // get outlines
594
664k
        out_it.set_to_list(blob->out_list());
595
1.66M
        for (out_it.mark_cycle_pt(); !out_it.cycled_list(); out_it.forward()) {
596
996k
          outline = out_it.data();
597
996k
          blob_box = outline->bounding_box();
598
996k
          blob_size = blob_box.width() > blob_box.height() ? blob_box.width() : blob_box.height();
599
996k
          if (blob_size < textord_noise_sizelimit * row->x_height()) {
600
394k
            dot_count++; // count small outlines
601
394k
          }
602
996k
          if (!outline->child()->empty() &&
603
40.7k
              blob_box.height() < (1 + textord_noise_syfract) * row->x_height() &&
604
35.5k
              blob_box.height() > (1 - textord_noise_syfract) * row->x_height() &&
605
11.6k
              blob_box.width() < (1 + textord_noise_sxfract) * row->x_height() &&
606
10.0k
              blob_box.width() > (1 - textord_noise_sxfract) * row->x_height()) {
607
4.35k
            norm_count++; // count small outlines
608
4.35k
          }
609
996k
        }
610
664k
      } else {
611
36.6k
        norm_count++;
612
36.6k
      }
613
701k
      blob_box = blob->bounding_box();
614
701k
      blob_size = blob_box.width() > blob_box.height() ? blob_box.width() : blob_box.height();
615
701k
      if (blob_size >= textord_noise_sizelimit * row->x_height() &&
616
588k
          blob_size < row->x_height() * 2) {
617
562k
        trans_threshold = blob_size / textord_noise_sizefraction;
618
562k
        trans_count = blob->count_transitions(trans_threshold);
619
562k
        if (trans_count < textord_noise_translimit) {
620
527k
          norm_count++;
621
527k
        }
622
562k
      } else if (blob_box.height() > row->x_height() * 2 &&
623
11.6k
                 (!word_it.at_first() || !blob_it.at_first())) {
624
6.60k
        dot_count += 2;
625
6.60k
      }
626
701k
    }
627
205k
    if (dot_count > 2 && !word->flag(W_REP_CHAR)) {
628
30.6k
      if (dot_count > norm_count * textord_noise_normratio * 2) {
629
4.59k
        word_dud[word_index] = 2;
630
26.0k
      } else if (dot_count > norm_count * textord_noise_normratio) {
631
3.69k
        word_dud[word_index] = 1;
632
22.3k
      } else {
633
22.3k
        word_dud[word_index] = 0;
634
22.3k
      }
635
175k
    } else {
636
175k
      word_dud[word_index] = 0;
637
175k
    }
638
205k
    if (word_dud[word_index] == 2) {
639
4.59k
      dud_words++;
640
201k
    } else {
641
201k
      ok_words++;
642
201k
    }
643
205k
    word_index++;
644
205k
  }
645
646
140k
  word_index = 0;
647
346k
  for (word_it.mark_cycle_pt(); !word_it.cycled_list(); word_it.forward()) {
648
205k
    if (word_dud[word_index] == 2 || (word_dud[word_index] == 1 && dud_words > ok_words)) {
649
4.83k
      word = word_it.data(); // Current word.
650
      // Previously we threw away the entire word.
651
      // Now just aggressively throw all small blobs into the reject list, where
652
      // the classifier can decide whether they are actually needed.
653
4.83k
      word->CleanNoise(textord_noise_sizelimit * row->x_height());
654
4.83k
    }
655
205k
    word_index++;
656
205k
  }
657
140k
}
658
659
// Remove outlines that are a tiny fraction in either width or height
660
// of the word height.
661
178k
void Textord::clean_small_noise_from_words(ROW *row) {
662
178k
  WERD_IT word_it(row->word_list());
663
468k
  for (word_it.mark_cycle_pt(); !word_it.cycled_list(); word_it.forward()) {
664
289k
    WERD *word = word_it.data();
665
289k
    int min_size = static_cast<int>(textord_noise_hfract * word->bounding_box().height() + 0.5);
666
289k
    C_BLOB_IT blob_it(word->cblob_list());
667
1.55M
    for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
668
1.26M
      C_BLOB *blob = blob_it.data();
669
1.26M
      C_OUTLINE_IT out_it(blob->out_list());
670
3.67M
      for (out_it.mark_cycle_pt(); !out_it.cycled_list(); out_it.forward()) {
671
2.40M
        C_OUTLINE *outline = out_it.data();
672
2.40M
        outline->RemoveSmallRecursive(min_size, &out_it);
673
2.40M
      }
674
1.26M
      if (blob->out_list()->empty()) {
675
162k
        delete blob_it.extract();
676
162k
      }
677
1.26M
    }
678
289k
    if (word->cblob_list()->empty()) {
679
6.87k
      if (!word_it.at_last()) {
680
        // The next word is no longer a fuzzy non space if it was before,
681
        // since the word before is about to be deleted.
682
936
        WERD *next_word = word_it.data_relative(1);
683
936
        if (next_word->flag(W_FUZZY_NON)) {
684
276
          next_word->set_flag(W_FUZZY_NON, false);
685
276
        }
686
936
      }
687
6.87k
      delete word_it.extract();
688
6.87k
    }
689
289k
  }
690
178k
}
691
692
// Local struct to hold a group of blocks.
693
struct BlockGroup {
694
0
  BlockGroup() : rotation(1.0f, 0.0f), angle(0.0f), min_xheight(1.0f) {}
695
  explicit BlockGroup(BLOCK *block)
696
14.4k
      : bounding_box(block->pdblk.bounding_box())
697
14.4k
      , rotation(block->re_rotation())
698
14.4k
      , angle(block->re_rotation().angle())
699
14.4k
      , min_xheight(block->x_height()) {
700
14.4k
    blocks.push_back(block);
701
14.4k
  }
702
  // Union of block bounding boxes.
703
  TBOX bounding_box;
704
  // Common rotation of the blocks.
705
  FCOORD rotation;
706
  // Angle of rotation.
707
  float angle;
708
  // Min xheight of the blocks.
709
  float min_xheight;
710
  // Collection of borrowed pointers to the blocks in the group.
711
  std::vector<BLOCK *> blocks;
712
};
713
714
// Groups blocks by rotation, then, for each group, makes a WordGrid and calls
715
// TransferDiacriticsToWords to copy the diacritic blobs to the most
716
// appropriate words in the group of blocks. Source blobs are not touched.
717
16.2k
void Textord::TransferDiacriticsToBlockGroups(BLOBNBOX_LIST *diacritic_blobs, BLOCK_LIST *blocks) {
718
  // Angle difference larger than this is too much to consider equal.
719
  // They should only be in multiples of M_PI/2 anyway.
720
16.2k
  const double kMaxAngleDiff = 0.01; // About 0.6 degrees.
721
16.2k
  std::vector<std::unique_ptr<BlockGroup>> groups;
722
16.2k
  BLOCK_IT bk_it(blocks);
723
30.6k
  for (bk_it.mark_cycle_pt(); !bk_it.cycled_list(); bk_it.forward()) {
724
14.4k
    BLOCK *block = bk_it.data();
725
14.4k
    if (block->pdblk.poly_block() != nullptr && !block->pdblk.poly_block()->IsText()) {
726
0
      continue;
727
0
    }
728
    // Linear search of the groups to find a matching rotation.
729
14.4k
    float block_angle = block->re_rotation().angle();
730
14.4k
    int best_g = 0;
731
14.4k
    float best_angle_diff = FLT_MAX;
732
14.4k
    for (const auto &group : groups) {
733
0
      double angle_diff = std::fabs(block_angle - group->angle);
734
0
      if (angle_diff > M_PI) {
735
0
        angle_diff = fabs(angle_diff - 2.0 * M_PI);
736
0
      }
737
0
      if (angle_diff < best_angle_diff) {
738
0
        best_angle_diff = angle_diff;
739
0
        best_g = &group - &groups[0];
740
0
      }
741
0
    }
742
14.4k
    if (best_angle_diff > kMaxAngleDiff) {
743
14.4k
      groups.push_back(std::make_unique<BlockGroup>(block));
744
14.4k
    } else {
745
0
      groups[best_g]->blocks.push_back(block);
746
0
      groups[best_g]->bounding_box += block->pdblk.bounding_box();
747
0
      float x_height = block->x_height();
748
0
      if (x_height < groups[best_g]->min_xheight) {
749
0
        groups[best_g]->min_xheight = x_height;
750
0
      }
751
0
    }
752
14.4k
  }
753
  // Now process each group of blocks.
754
16.2k
  std::vector<std::unique_ptr<WordWithBox>> word_ptrs;
755
16.2k
  for (const auto &group : groups) {
756
14.4k
    if (group->bounding_box.null_box()) {
757
0
      continue;
758
0
    }
759
14.4k
    WordGrid word_grid(group->min_xheight, group->bounding_box.botleft(),
760
14.4k
                       group->bounding_box.topright());
761
14.4k
    for (auto b : group->blocks) {
762
14.4k
      ROW_IT row_it(b->row_list());
763
155k
      for (row_it.mark_cycle_pt(); !row_it.cycled_list(); row_it.forward()) {
764
140k
        ROW *row = row_it.data();
765
        // Put the words of the row into the grid.
766
140k
        WERD_IT w_it(row->word_list());
767
346k
        for (w_it.mark_cycle_pt(); !w_it.cycled_list(); w_it.forward()) {
768
205k
          WERD *word = w_it.data();
769
205k
          auto box_word = std::make_unique<WordWithBox>(word);
770
205k
          word_grid.InsertBBox(true, true, box_word.get());
771
          // Save the pointer where it will be auto-deleted.
772
205k
          word_ptrs.emplace_back(std::move(box_word));
773
205k
        }
774
140k
      }
775
14.4k
    }
776
14.4k
    FCOORD rotation = group->rotation;
777
    // Make it a forward rotation that will transform blob coords to block.
778
14.4k
    rotation.set_y(-rotation.y());
779
14.4k
    TransferDiacriticsToWords(diacritic_blobs, rotation, &word_grid);
780
14.4k
  }
781
16.2k
}
782
783
// Places a copy of blobs that are near a word (after applying rotation to the
784
// blob) in the most appropriate word, unless there is doubt, in which case a
785
// blob can end up in two words. Source blobs are not touched.
786
void Textord::TransferDiacriticsToWords(BLOBNBOX_LIST *diacritic_blobs, const FCOORD &rotation,
787
14.4k
                                        WordGrid *word_grid) {
788
14.4k
  WordSearch ws(word_grid);
789
14.4k
  BLOBNBOX_IT b_it(diacritic_blobs);
790
  // Apply rotation to each blob before finding the nearest words. The rotation
791
  // allows us to only consider above/below placement and not left/right on
792
  // vertical text, because all text is horizontal here.
793
14.4k
  for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
794
0
    BLOBNBOX *blobnbox = b_it.data();
795
0
    TBOX blob_box = blobnbox->bounding_box();
796
0
    blob_box.rotate(rotation);
797
0
    ws.StartRectSearch(blob_box);
798
    // Above/below refer to word position relative to diacritic. Since some
799
    // scripts eg Kannada/Telugu habitually put diacritics below words, and
800
    // others eg Thai/Vietnamese/Latin put most diacritics above words, try
801
    // for both if there isn't much in it.
802
0
    WordWithBox *best_above_word = nullptr;
803
0
    WordWithBox *best_below_word = nullptr;
804
0
    int best_above_distance = 0;
805
0
    int best_below_distance = 0;
806
0
    for (WordWithBox *word = ws.NextRectSearch(); word != nullptr; word = ws.NextRectSearch()) {
807
0
      if (word->word()->flag(W_REP_CHAR)) {
808
0
        continue;
809
0
      }
810
0
      TBOX word_box = word->true_bounding_box();
811
0
      int x_distance = blob_box.x_gap(word_box);
812
0
      int y_distance = blob_box.y_gap(word_box);
813
0
      if (x_distance > 0) {
814
        // Arbitrarily divide x-distance by 2 if there is a major y overlap,
815
        // and the word is to the left of the diacritic. If the
816
        // diacritic is a dropped broken character between two words, this will
817
        // help send all the pieces to a single word, instead of splitting them
818
        // over the 2 words.
819
0
        if (word_box.major_y_overlap(blob_box) && blob_box.left() > word_box.right()) {
820
0
          x_distance /= 2;
821
0
        }
822
0
        y_distance += x_distance;
823
0
      }
824
0
      if (word_box.y_middle() > blob_box.y_middle() &&
825
0
          (best_above_word == nullptr || y_distance < best_above_distance)) {
826
0
        best_above_word = word;
827
0
        best_above_distance = y_distance;
828
0
      }
829
0
      if (word_box.y_middle() <= blob_box.y_middle() &&
830
0
          (best_below_word == nullptr || y_distance < best_below_distance)) {
831
0
        best_below_word = word;
832
0
        best_below_distance = y_distance;
833
0
      }
834
0
    }
835
0
    bool above_good = best_above_word != nullptr &&
836
0
                      (best_below_word == nullptr ||
837
0
                       best_above_distance < best_below_distance + blob_box.height());
838
0
    bool below_good = best_below_word != nullptr && best_below_word != best_above_word &&
839
0
                      (best_above_word == nullptr ||
840
0
                       best_below_distance < best_above_distance + blob_box.height());
841
0
    if (below_good) {
842
0
      C_BLOB *copied_blob = C_BLOB::deep_copy(blobnbox->cblob());
843
0
      copied_blob->rotate(rotation);
844
      // Put the blob into the word's reject blobs list.
845
0
      C_BLOB_IT blob_it(best_below_word->RejBlobs());
846
0
      blob_it.add_to_end(copied_blob);
847
0
    }
848
0
    if (above_good) {
849
0
      C_BLOB *copied_blob = C_BLOB::deep_copy(blobnbox->cblob());
850
0
      copied_blob->rotate(rotation);
851
      // Put the blob into the word's reject blobs list.
852
0
      C_BLOB_IT blob_it(best_above_word->RejBlobs());
853
0
      blob_it.add_to_end(copied_blob);
854
0
    }
855
0
  }
856
14.4k
}
857
858
/**********************************************************************
859
 * tweak_row_baseline
860
 *
861
 * Shift baseline to fit the blobs more accurately where they are
862
 * close enough.
863
 **********************************************************************/
864
865
140k
void tweak_row_baseline(ROW *row, double blshift_maxshift, double blshift_xfraction) {
866
140k
  TBOX blob_box;      // bounding box
867
140k
  C_BLOB *blob;       // current blob
868
140k
  WERD *word;         // current word
869
140k
  int32_t blob_count; // no of blobs
870
140k
  int32_t src_index;  // source segment
871
140k
  int32_t dest_index; // destination segment
872
140k
  float ydiff;        // baseline error
873
140k
  float x_centre;     // centre of blob
874
                      // words of row
875
140k
  WERD_IT word_it = row->word_list();
876
140k
  C_BLOB_IT blob_it; // blob iterator
877
878
140k
  blob_count = 0;
879
346k
  for (word_it.mark_cycle_pt(); !word_it.cycled_list(); word_it.forward()) {
880
205k
    word = word_it.data(); // current word
881
                           // get total blobs
882
205k
    blob_count += word->cblob_list()->length();
883
205k
  }
884
140k
  if (blob_count == 0) {
885
0
    return;
886
0
  }
887
  // spline segments
888
140k
  std::vector<int32_t> xstarts(blob_count + row->baseline.segments + 1);
889
  // spline coeffs
890
140k
  std::vector<double> coeffs((blob_count + row->baseline.segments) * 3);
891
892
140k
  src_index = 0;
893
140k
  dest_index = 0;
894
140k
  xstarts[0] = row->baseline.xcoords[0];
895
346k
  for (word_it.mark_cycle_pt(); !word_it.cycled_list(); word_it.forward()) {
896
205k
    word = word_it.data(); // current word
897
                           // blobs in word
898
205k
    blob_it.set_to_list(word->cblob_list());
899
890k
    for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
900
684k
      blob = blob_it.data();
901
684k
      blob_box = blob->bounding_box();
902
684k
      x_centre = (blob_box.left() + blob_box.right()) / 2.0;
903
684k
      ydiff = blob_box.bottom() - row->base_line(x_centre);
904
684k
      if (ydiff < 0) {
905
239k
        ydiff = -ydiff / row->x_height();
906
444k
      } else {
907
444k
        ydiff = ydiff / row->x_height();
908
444k
      }
909
684k
      if (ydiff < blshift_maxshift && blob_box.height() / row->x_height() > blshift_xfraction) {
910
0
        if (xstarts[dest_index] >= x_centre) {
911
0
          xstarts[dest_index] = blob_box.left();
912
0
        }
913
0
        coeffs[dest_index * 3] = 0;
914
0
        coeffs[dest_index * 3 + 1] = 0;
915
0
        coeffs[dest_index * 3 + 2] = blob_box.bottom();
916
        // shift it
917
0
        dest_index++;
918
0
        xstarts[dest_index] = blob_box.right() + 1;
919
684k
      } else {
920
684k
        if (xstarts[dest_index] <= x_centre) {
921
392k
          while (row->baseline.xcoords[src_index + 1] <= x_centre &&
922
157k
                 src_index < row->baseline.segments - 1) {
923
157k
            if (row->baseline.xcoords[src_index + 1] > xstarts[dest_index]) {
924
63.0k
              coeffs[dest_index * 3] = row->baseline.quadratics[src_index].a;
925
63.0k
              coeffs[dest_index * 3 + 1] = row->baseline.quadratics[src_index].b;
926
63.0k
              coeffs[dest_index * 3 + 2] = row->baseline.quadratics[src_index].c;
927
63.0k
              dest_index++;
928
63.0k
              xstarts[dest_index] = row->baseline.xcoords[src_index + 1];
929
63.0k
            }
930
157k
            src_index++;
931
157k
          }
932
235k
          coeffs[dest_index * 3] = row->baseline.quadratics[src_index].a;
933
235k
          coeffs[dest_index * 3 + 1] = row->baseline.quadratics[src_index].b;
934
235k
          coeffs[dest_index * 3 + 2] = row->baseline.quadratics[src_index].c;
935
235k
          dest_index++;
936
235k
          xstarts[dest_index] = row->baseline.xcoords[src_index + 1];
937
235k
        }
938
684k
      }
939
684k
    }
940
205k
  }
941
281k
  while (src_index < row->baseline.segments &&
942
256k
         row->baseline.xcoords[src_index + 1] <= xstarts[dest_index]) {
943
140k
    src_index++;
944
140k
  }
945
259k
  while (src_index < row->baseline.segments) {
946
118k
    coeffs[dest_index * 3] = row->baseline.quadratics[src_index].a;
947
118k
    coeffs[dest_index * 3 + 1] = row->baseline.quadratics[src_index].b;
948
118k
    coeffs[dest_index * 3 + 2] = row->baseline.quadratics[src_index].c;
949
118k
    dest_index++;
950
118k
    src_index++;
951
118k
    xstarts[dest_index] = row->baseline.xcoords[src_index];
952
118k
  }
953
  // turn to spline
954
140k
  row->baseline = QSPLINE(dest_index, &xstarts[0], &coeffs[0]);
955
140k
}
956
957
} // namespace tesseract