Coverage Report

Created: 2026-06-13 06:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tesseract/src/ccstruct/coutln.cpp
Line
Count
Source
1
/**********************************************************************
2
 * File:        coutln.cpp  (Formerly coutline.c)
3
 * Description: Code for the C_OUTLINE class.
4
 * Author:      Ray Smith
5
 *
6
 * (C) Copyright 1991, 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
// Include automatically generated configuration file if running autoconf.
20
#ifdef HAVE_CONFIG_H
21
#  include "config_auto.h"
22
#endif
23
24
#include "coutln.h"
25
26
#include "arrayaccess.h" // for GET_DATA_BYTE
27
#include "blobs.h"       // for TPOINT
28
#include "crakedge.h"    // for CRACKEDGE
29
#include "environ.h"     // for l_uint32
30
#include "errcode.h"     // for ASSERT_HOST
31
#include "normalis.h"    // for DENORM
32
33
#include "helpers.h" // for ClipToRange, IntCastRounded, Modulo
34
35
#include <allheaders.h> // for pixSetPixel, pixGetData, pixRasterop, pixGe...
36
#include "pix.h"        // for Pix (ptr only), PIX_DST, PIX_NOT
37
38
#include <algorithm> // for max, min
39
#include <cmath>     // for abs
40
#include <cstdlib>   // for abs
41
#include <cstring>   // for memset, memcpy, memmove
42
43
namespace tesseract {
44
45
ICOORD C_OUTLINE::step_coords[4] = {ICOORD(-1, 0), ICOORD(0, -1), ICOORD(1, 0), ICOORD(0, 1)};
46
47
/**
48
 * @name C_OUTLINE::C_OUTLINE
49
 *
50
 * Constructor to build a C_OUTLINE from a CRACKEDGE LOOP.
51
 * @param startpt outline to convert
52
 * @param bot_left bounding box
53
 * @param top_right bounding box
54
 * @param length length of loop
55
 */
56
57
C_OUTLINE::C_OUTLINE(CRACKEDGE *startpt, ICOORD bot_left, ICOORD top_right, int16_t length)
58
3.00M
    : box(bot_left, top_right), start(startpt->pos), offsets(nullptr) {
59
3.00M
  int16_t stepindex; // index to step
60
3.00M
  CRACKEDGE *edgept; // current point
61
62
3.00M
  stepcount = length; // no of steps
63
3.00M
  if (length == 0) {
64
188k
    return;
65
188k
  }
66
  // get memory
67
2.81M
  steps.resize(step_mem());
68
2.81M
  edgept = startpt;
69
70
83.9M
  for (stepindex = 0; stepindex < length; stepindex++) {
71
    // set compact step
72
81.1M
    set_step(stepindex, edgept->stepdir);
73
81.1M
    edgept = edgept->next;
74
81.1M
  }
75
2.81M
}
76
77
/**
78
 * @name C_OUTLINE::C_OUTLINE
79
 *
80
 * Constructor to build a C_OUTLINE from a C_OUTLINE_FRAG.
81
 */
82
C_OUTLINE::C_OUTLINE(
83
    // constructor
84
    // steps to copy
85
    ICOORD startpt, DIR128 *new_steps,
86
    int16_t length // length of loop
87
    )
88
172k
    : start(startpt), offsets(nullptr) {
89
172k
  int8_t dirdiff;    // direction difference
90
172k
  DIR128 prevdir;    // previous direction
91
172k
  DIR128 dir;        // current direction
92
172k
  DIR128 lastdir;    // dir of last step
93
172k
  TBOX new_box;      // easy bounding
94
172k
  int16_t stepindex; // index to step
95
172k
  int16_t srcindex;  // source steps
96
172k
  ICOORD pos;        // current position
97
98
172k
  pos = startpt;
99
172k
  stepcount = length; // No. of steps.
100
172k
  ASSERT_HOST(length >= 0);
101
172k
  steps.resize(step_mem()); // Get memory.
102
103
172k
  lastdir = new_steps[length - 1];
104
172k
  prevdir = lastdir;
105
24.2M
  for (stepindex = 0, srcindex = 0; srcindex < length; stepindex++, srcindex++) {
106
24.1M
    new_box = TBOX(pos, pos);
107
24.1M
    box += new_box;
108
    // copy steps
109
24.1M
    dir = new_steps[srcindex];
110
24.1M
    set_step(stepindex, dir);
111
24.1M
    dirdiff = dir - prevdir;
112
24.1M
    pos += step(stepindex);
113
24.1M
    if ((dirdiff == 64 || dirdiff == -64) && stepindex > 0) {
114
0
      stepindex -= 2; // cancel there-and-back
115
0
      prevdir = stepindex >= 0 ? step_dir(stepindex) : lastdir;
116
24.1M
    } else {
117
24.1M
      prevdir = dir;
118
24.1M
    }
119
24.1M
  }
120
172k
  ASSERT_HOST(pos.x() == startpt.x() && pos.y() == startpt.y());
121
172k
  do {
122
172k
    dirdiff = step_dir(stepindex - 1) - step_dir(0);
123
172k
    if (dirdiff == 64 || dirdiff == -64) {
124
0
      start += step(0);
125
0
      stepindex -= 2; // cancel there-and-back
126
0
      for (int i = 0; i < stepindex; ++i) {
127
0
        set_step(i, step_dir(i + 1));
128
0
      }
129
0
    }
130
172k
  } while (stepindex > 1 && (dirdiff == 64 || dirdiff == -64));
131
172k
  stepcount = stepindex;
132
172k
  ASSERT_HOST(stepcount >= 4);
133
172k
}
134
135
/**
136
 * @name C_OUTLINE::C_OUTLINE
137
 *
138
 * Constructor to build a C_OUTLINE from a rotation of a C_OUTLINE.
139
 * @param srcline outline to rotate
140
 * @param rotation rotate to coord
141
 */
142
143
60.6k
C_OUTLINE::C_OUTLINE(C_OUTLINE *srcline, FCOORD rotation) : offsets(nullptr) {
144
60.6k
  TBOX new_box;      // easy bounding
145
60.6k
  int16_t stepindex; // index to step
146
60.6k
  int16_t dirdiff;   // direction change
147
60.6k
  ICOORD pos;        // current position
148
60.6k
  ICOORD prevpos;    // previous dest point
149
150
60.6k
  ICOORD destpos;                // destination point
151
60.6k
  int16_t destindex = INT16_MAX; // index to step
152
60.6k
  DIR128 dir;                    // coded direction
153
60.6k
  uint8_t new_step;
154
155
60.6k
  stepcount = srcline->stepcount * 2;
156
60.6k
  if (stepcount == 0) {
157
0
    box = srcline->box;
158
0
    box.rotate(rotation);
159
0
    return;
160
0
  }
161
  // get memory
162
60.6k
  steps.resize(step_mem());
163
164
60.7k
  for (int iteration = 0; iteration < 2; ++iteration) {
165
60.7k
    DIR128 round1 = iteration == 0 ? 32 : 0;
166
60.7k
    DIR128 round2 = iteration != 0 ? 32 : 0;
167
60.7k
    pos = srcline->start;
168
60.7k
    prevpos = pos;
169
60.7k
    prevpos.rotate(rotation);
170
60.7k
    start = prevpos;
171
60.7k
    box = TBOX(start, start);
172
60.7k
    destindex = 0;
173
15.7M
    for (stepindex = 0; stepindex < srcline->stepcount; stepindex++) {
174
15.6M
      pos += srcline->step(stepindex);
175
15.6M
      destpos = pos;
176
15.6M
      destpos.rotate(rotation);
177
      //  tprintf("%i %i %i %i ", destpos.x(), destpos.y(), pos.x(), pos.y());
178
31.3M
      while (destpos.x() != prevpos.x() || destpos.y() != prevpos.y()) {
179
15.6M
        dir = DIR128(FCOORD(destpos - prevpos));
180
15.6M
        dir += 64; // turn to step style
181
15.6M
        new_step = dir.get_dir();
182
        //  tprintf(" %i\n", new_step);
183
15.6M
        if (new_step & 31) {
184
156k
          set_step(destindex++, dir + round1);
185
156k
          prevpos += step(destindex - 1);
186
156k
          if (destindex < 2 ||
187
155k
              ((dirdiff = step_dir(destindex - 1) - step_dir(destindex - 2)) != -64 &&
188
142k
               dirdiff != 64)) {
189
128k
            set_step(destindex++, dir + round2);
190
128k
            prevpos += step(destindex - 1);
191
128k
          } else {
192
27.7k
            prevpos -= step(destindex - 1);
193
27.7k
            destindex--;
194
27.7k
            prevpos -= step(destindex - 1);
195
27.7k
            set_step(destindex - 1, dir + round2);
196
27.7k
            prevpos += step(destindex - 1);
197
27.7k
          }
198
15.5M
        } else {
199
15.5M
          set_step(destindex++, dir);
200
15.5M
          prevpos += step(destindex - 1);
201
15.5M
        }
202
15.6M
        while (destindex >= 2 &&
203
15.6M
               ((dirdiff = step_dir(destindex - 1) - step_dir(destindex - 2)) == -64 ||
204
15.6M
                dirdiff == 64)) {
205
27.4k
          prevpos -= step(destindex - 1);
206
27.4k
          prevpos -= step(destindex - 2);
207
27.4k
          destindex -= 2; // Forget u turn
208
27.4k
        }
209
        // ASSERT_HOST(prevpos.x() == destpos.x() && prevpos.y() ==
210
        // destpos.y());
211
15.6M
        new_box = TBOX(destpos, destpos);
212
15.6M
        box += new_box;
213
15.6M
      }
214
15.6M
    }
215
60.7k
    ASSERT_HOST(destpos.x() == start.x() && destpos.y() == start.y());
216
61.9k
    while (destindex > 1) {
217
61.9k
      dirdiff = step_dir(destindex - 1) - step_dir(0);
218
61.9k
      if (dirdiff != 64 && dirdiff != -64) {
219
60.6k
        break;
220
60.6k
      }
221
1.21k
      start += step(0);
222
1.21k
      destindex -= 2;
223
299k
      for (int i = 0; i < destindex; ++i) {
224
298k
        set_step(i, step_dir(i + 1));
225
298k
      }
226
1.21k
    }
227
60.7k
    if (destindex >= 4) {
228
60.6k
      break;
229
60.6k
    }
230
60.7k
  }
231
60.6k
  ASSERT_HOST(destindex <= stepcount);
232
60.6k
  stepcount = destindex;
233
60.6k
  destpos = start;
234
15.7M
  for (stepindex = 0; stepindex < stepcount; stepindex++) {
235
15.7M
    destpos += step(stepindex);
236
15.7M
  }
237
60.6k
  ASSERT_HOST(destpos.x() == start.x() && destpos.y() == start.y());
238
60.6k
}
239
240
// Build a fake outline, given just a bounding box and append to the list.
241
188k
void C_OUTLINE::FakeOutline(const TBOX &box, C_OUTLINE_LIST *outlines) {
242
188k
  C_OUTLINE_IT ol_it(outlines);
243
  // Make a C_OUTLINE from the bounds. This is a bit of a hack,
244
  // as there is no outline, just a bounding box, but it works nicely.
245
188k
  CRACKEDGE start;
246
188k
  start.pos = box.topleft();
247
188k
  auto *outline = new C_OUTLINE(&start, box.topleft(), box.botright(), 0);
248
188k
  ol_it.add_to_end(outline);
249
188k
}
250
251
/**
252
 * @name C_OUTLINE::area
253
 *
254
 * Compute the area of the outline.
255
 */
256
257
2.87M
int32_t C_OUTLINE::area() const {
258
2.87M
  int stepindex;       // current step
259
2.87M
  int32_t total_steps; // steps to do
260
2.87M
  int32_t total;       // total area
261
2.87M
  ICOORD pos;          // position of point
262
2.87M
  ICOORD next_step;    // step to next pix
263
  // We aren't going to modify the list, or its contents, but there is
264
  // no const iterator.
265
2.87M
  C_OUTLINE_IT it(const_cast<C_OUTLINE_LIST *>(&children));
266
267
2.87M
  pos = start_pos();
268
2.87M
  total_steps = pathlength();
269
2.87M
  total = 0;
270
88.7M
  for (stepindex = 0; stepindex < total_steps; stepindex++) {
271
    // all intersected
272
85.8M
    next_step = step(stepindex);
273
85.8M
    if (next_step.x() < 0) {
274
20.0M
      total += pos.y();
275
65.8M
    } else if (next_step.x() > 0) {
276
20.0M
      total -= pos.y();
277
20.0M
    }
278
85.8M
    pos += next_step;
279
85.8M
  }
280
3.11M
  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
281
238k
    total += it.data()->area(); // add areas of children
282
238k
  }
283
284
2.87M
  return total;
285
2.87M
}
286
287
/**
288
 * @name C_OUTLINE::perimeter
289
 *
290
 * Compute the perimeter of the outline and its first level children.
291
 */
292
293
2.36M
int32_t C_OUTLINE::perimeter() const {
294
2.36M
  int32_t total_steps; // Return value.
295
  // We aren't going to modify the list, or its contents, but there is
296
  // no const iterator.
297
2.36M
  C_OUTLINE_IT it(const_cast<C_OUTLINE_LIST *>(&children));
298
299
2.36M
  total_steps = pathlength();
300
2.55M
  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
301
189k
    total_steps += it.data()->pathlength(); // Add perimeters of children.
302
189k
  }
303
304
2.36M
  return total_steps;
305
2.36M
}
306
307
/**
308
 * @name C_OUTLINE::outer_area
309
 *
310
 * Compute the area of the outline.
311
 */
312
313
3.15M
int32_t C_OUTLINE::outer_area() const {
314
3.15M
  int stepindex;       // current step
315
3.15M
  int32_t total_steps; // steps to do
316
3.15M
  int32_t total;       // total area
317
3.15M
  ICOORD pos;          // position of point
318
3.15M
  ICOORD next_step;    // step to next pix
319
320
3.15M
  pos = start_pos();
321
3.15M
  total_steps = pathlength();
322
3.15M
  if (total_steps == 0) {
323
0
    return box.area();
324
0
  }
325
3.15M
  total = 0;
326
109M
  for (stepindex = 0; stepindex < total_steps; stepindex++) {
327
    // all intersected
328
106M
    next_step = step(stepindex);
329
106M
    if (next_step.x() < 0) {
330
25.5M
      total += pos.y();
331
80.9M
    } else if (next_step.x() > 0) {
332
25.5M
      total -= pos.y();
333
25.5M
    }
334
106M
    pos += next_step;
335
106M
  }
336
337
3.15M
  return total;
338
3.15M
}
339
340
/**
341
 * @name C_OUTLINE::count_transitions
342
 *
343
 * Compute the number of x and y maxes and mins in the outline.
344
 * @param threshold winding number on size
345
 */
346
347
2.17M
int32_t C_OUTLINE::count_transitions(int32_t threshold) {
348
2.17M
  bool first_was_max_x; // what was first
349
2.17M
  bool first_was_max_y;
350
2.17M
  bool looking_for_max_x; // what is next
351
2.17M
  bool looking_for_min_x;
352
2.17M
  bool looking_for_max_y; // what is next
353
2.17M
  bool looking_for_min_y;
354
2.17M
  int stepindex;       // current step
355
2.17M
  int32_t total_steps; // steps to do
356
                       // current limits
357
2.17M
  int32_t max_x, min_x, max_y, min_y;
358
2.17M
  int32_t initial_x, initial_y; // initial limits
359
2.17M
  int32_t total;                // total changes
360
2.17M
  ICOORD pos;                   // position of point
361
2.17M
  ICOORD next_step;             // step to next pix
362
363
2.17M
  pos = start_pos();
364
2.17M
  total_steps = pathlength();
365
2.17M
  total = 0;
366
2.17M
  max_x = min_x = pos.x();
367
2.17M
  max_y = min_y = pos.y();
368
2.17M
  looking_for_max_x = true;
369
2.17M
  looking_for_min_x = true;
370
2.17M
  looking_for_max_y = true;
371
2.17M
  looking_for_min_y = true;
372
2.17M
  first_was_max_x = false;
373
2.17M
  first_was_max_y = false;
374
2.17M
  initial_x = pos.x();
375
2.17M
  initial_y = pos.y(); // stop uninit warning
376
55.6M
  for (stepindex = 0; stepindex < total_steps; stepindex++) {
377
    // all intersected
378
53.4M
    next_step = step(stepindex);
379
53.4M
    pos += next_step;
380
53.4M
    if (next_step.x() < 0) {
381
10.6M
      if (looking_for_max_x && pos.x() < min_x) {
382
4.74M
        min_x = pos.x();
383
4.74M
      }
384
10.6M
      if (looking_for_min_x && max_x - pos.x() > threshold) {
385
2.53M
        if (looking_for_max_x) {
386
354k
          initial_x = max_x;
387
354k
          first_was_max_x = false;
388
354k
        }
389
2.53M
        total++;
390
2.53M
        looking_for_max_x = true;
391
2.53M
        looking_for_min_x = false;
392
2.53M
        min_x = pos.x(); // reset min
393
2.53M
      }
394
42.8M
    } else if (next_step.x() > 0) {
395
10.6M
      if (looking_for_min_x && pos.x() > max_x) {
396
7.10M
        max_x = pos.x();
397
7.10M
      }
398
10.6M
      if (looking_for_max_x && pos.x() - min_x > threshold) {
399
2.37M
        if (looking_for_min_x) {
400
1.27M
          initial_x = min_x; // remember first min
401
1.27M
          first_was_max_x = true;
402
1.27M
        }
403
2.37M
        total++;
404
2.37M
        looking_for_max_x = false;
405
2.37M
        looking_for_min_x = true;
406
2.37M
        max_x = pos.x();
407
2.37M
      }
408
32.1M
    } else if (next_step.y() < 0) {
409
16.0M
      if (looking_for_max_y && pos.y() < min_y) {
410
14.0M
        min_y = pos.y();
411
14.0M
      }
412
16.0M
      if (looking_for_min_y && max_y - pos.y() > threshold) {
413
2.47M
        if (looking_for_max_y) {
414
1.94M
          initial_y = max_y; // remember first max
415
1.94M
          first_was_max_y = false;
416
1.94M
        }
417
2.47M
        total++;
418
2.47M
        looking_for_max_y = true;
419
2.47M
        looking_for_min_y = false;
420
2.47M
        min_y = pos.y(); // reset min
421
2.47M
      }
422
16.0M
    } else {
423
16.0M
      if (looking_for_min_y && pos.y() > max_y) {
424
9.98M
        max_y = pos.y();
425
9.98M
      }
426
16.0M
      if (looking_for_max_y && pos.y() - min_y > threshold) {
427
2.48M
        if (looking_for_min_y) {
428
12.4k
          initial_y = min_y; // remember first min
429
12.4k
          first_was_max_y = true;
430
12.4k
        }
431
2.48M
        total++;
432
2.48M
        looking_for_max_y = false;
433
2.48M
        looking_for_min_y = true;
434
2.48M
        max_y = pos.y();
435
2.48M
      }
436
16.0M
    }
437
53.4M
  }
438
2.17M
  if (first_was_max_x && looking_for_min_x) {
439
118k
    if (max_x - initial_x > threshold) {
440
118k
      total++;
441
118k
    } else {
442
0
      total--;
443
0
    }
444
2.05M
  } else if (!first_was_max_x && looking_for_max_x) {
445
824k
    if (initial_x - min_x > threshold) {
446
0
      total++;
447
824k
    } else {
448
824k
      total--;
449
824k
    }
450
824k
  }
451
2.17M
  if (first_was_max_y && looking_for_min_y) {
452
8.59k
    if (max_y - initial_y > threshold) {
453
450
      total++;
454
8.14k
    } else {
455
8.14k
      total--;
456
8.14k
    }
457
2.16M
  } else if (!first_was_max_y && looking_for_max_y) {
458
213k
    if (initial_y - min_y > threshold) {
459
411
      total++;
460
213k
    } else {
461
213k
      total--;
462
213k
    }
463
213k
  }
464
465
2.17M
  return total;
466
2.17M
}
467
468
/**
469
 * @name C_OUTLINE::operator<
470
 *
471
 * @return true if the left operand is inside the right one.
472
 * @param other other outline
473
 */
474
475
36.7M
bool C_OUTLINE::operator<(const C_OUTLINE &other) const {
476
36.7M
  int16_t count = 0; // winding count
477
36.7M
  ICOORD pos;        // position of point
478
36.7M
  int32_t stepindex; // index to cstep
479
480
36.7M
  if (!box.overlap(other.box)) {
481
32.7M
    return false; // can't be contained
482
32.7M
  }
483
4.06M
  if (stepcount == 0) {
484
0
    return other.box.contains(this->box);
485
0
  }
486
487
4.06M
  pos = start;
488
4.66M
  for (stepindex = 0; stepindex < stepcount && (count = other.winding_number(pos)) == INTERSECTING;
489
4.06M
       stepindex++) {
490
593k
    pos += step(stepindex); // try all points
491
593k
  }
492
4.06M
  if (count == INTERSECTING) {
493
    // all intersected
494
0
    pos = other.start;
495
0
    for (stepindex = 0;
496
0
         stepindex < other.stepcount && (count = winding_number(pos)) == INTERSECTING;
497
0
         stepindex++) {
498
      // try other way round
499
0
      pos += other.step(stepindex);
500
0
    }
501
0
    return count == INTERSECTING || count == 0;
502
0
  }
503
4.06M
  return count != 0;
504
4.06M
}
505
506
/**
507
 * @name C_OUTLINE::winding_number
508
 *
509
 * @return the winding number of the outline around the given point.
510
 * @param point point to wind around
511
 */
512
513
4.66M
int16_t C_OUTLINE::winding_number(ICOORD point) const {
514
4.66M
  int16_t stepindex; // index to cstep
515
4.66M
  int16_t count;     // winding count
516
4.66M
  ICOORD vec;        // to current point
517
4.66M
  ICOORD stepvec;    // step vector
518
4.66M
  int32_t cross;     // cross product
519
520
4.66M
  vec = start - point; // vector to it
521
4.66M
  count = 0;
522
1.52G
  for (stepindex = 0; stepindex < stepcount; stepindex++) {
523
1.52G
    stepvec = step(stepindex); // get the step
524
                               // crossing the line
525
1.52G
    if (vec.y() <= 0 && vec.y() + stepvec.y() > 0) {
526
10.5M
      cross = vec * stepvec; // cross product
527
10.5M
      if (cross > 0) {
528
6.24M
        count++; // crossing right half
529
6.24M
      } else if (cross == 0) {
530
98.5k
        return INTERSECTING; // going through point
531
98.5k
      }
532
1.51G
    } else if (vec.y() > 0 && vec.y() + stepvec.y() <= 0) {
533
11.0M
      cross = vec * stepvec;
534
11.0M
      if (cross < 0) {
535
5.42M
        count--; // crossing back
536
5.59M
      } else if (cross == 0) {
537
494k
        return INTERSECTING; // illegal
538
494k
      }
539
11.0M
    }
540
1.52G
    vec += stepvec; // sum vectors
541
1.52G
  }
542
4.06M
  return count; // winding number
543
4.66M
}
544
545
/**
546
 * C_OUTLINE::turn_direction
547
 *
548
 * @return the sum direction delta of the outline.
549
 */
550
551
2.97M
int16_t C_OUTLINE::turn_direction() const { // winding number
552
2.97M
  DIR128 prevdir;                           // previous direction
553
2.97M
  DIR128 dir;                               // current direction
554
2.97M
  int16_t stepindex;                        // index to cstep
555
2.97M
  int8_t dirdiff;                           // direction difference
556
2.97M
  int16_t count;                            // winding count
557
558
2.97M
  if (stepcount == 0) {
559
188k
    return 128;
560
188k
  }
561
2.78M
  count = 0;
562
2.78M
  prevdir = step_dir(stepcount - 1);
563
102M
  for (stepindex = 0; stepindex < stepcount; stepindex++) {
564
100M
    dir = step_dir(stepindex);
565
100M
    dirdiff = dir - prevdir;
566
100M
    ASSERT_HOST(dirdiff == 0 || dirdiff == 32 || dirdiff == -32);
567
100M
    count += dirdiff;
568
100M
    prevdir = dir;
569
100M
  }
570
2.78M
  ASSERT_HOST(count == 128 || count == -128);
571
2.78M
  return count; // winding number
572
2.97M
}
573
574
/**
575
 * @name C_OUTLINE::reverse
576
 *
577
 * Reverse the direction of an outline.
578
 */
579
580
940k
void C_OUTLINE::reverse() {      // reverse direction
581
940k
  DIR128 halfturn = MODULUS / 2; // amount to shift
582
940k
  DIR128 stepdir;                // direction of step
583
940k
  int16_t stepindex;             // index to cstep
584
940k
  int16_t farindex;              // index to other side
585
940k
  int16_t halfsteps;             // half of stepcount
586
587
940k
  halfsteps = (stepcount + 1) / 2;
588
11.3M
  for (stepindex = 0; stepindex < halfsteps; stepindex++) {
589
10.4M
    farindex = stepcount - stepindex - 1;
590
10.4M
    stepdir = step_dir(stepindex);
591
10.4M
    set_step(stepindex, step_dir(farindex) + halfturn);
592
10.4M
    set_step(farindex, stepdir + halfturn);
593
10.4M
  }
594
940k
}
595
596
/**
597
 * @name C_OUTLINE::move
598
 *
599
 * Move C_OUTLINE by vector
600
 * @param vec vector to reposition OUTLINE by
601
 */
602
603
0
void C_OUTLINE::move(const ICOORD vec) {
604
0
  C_OUTLINE_IT it(&children); // iterator
605
606
0
  box.move(vec);
607
0
  start += vec;
608
609
0
  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
610
0
    it.data()->move(vec); // move child outlines
611
0
  }
612
0
}
613
614
/**
615
 * Returns true if *this and its children are legally nested.
616
 * The outer area of a child should have the opposite sign to the
617
 * parent. If not, it means we have discarded an outline in between
618
 * (probably due to excessive length).
619
 */
620
2.81M
bool C_OUTLINE::IsLegallyNested() const {
621
2.81M
  if (stepcount == 0) {
622
0
    return true;
623
0
  }
624
2.81M
  int64_t parent_area = outer_area();
625
  // We aren't going to modify the list, or its contents, but there is
626
  // no const iterator.
627
2.81M
  C_OUTLINE_IT child_it(const_cast<C_OUTLINE_LIST *>(&children));
628
3.02M
  for (child_it.mark_cycle_pt(); !child_it.cycled_list(); child_it.forward()) {
629
216k
    const C_OUTLINE *child = child_it.data();
630
216k
    if (child->outer_area() * parent_area > 0 || !child->IsLegallyNested()) {
631
0
      return false;
632
0
    }
633
216k
  }
634
2.81M
  return true;
635
2.81M
}
636
637
/**
638
 * If this outline is smaller than the given min_size, delete this and
639
 * remove from its list, via *it, after checking that *it points to this.
640
 * Otherwise, if any children of this are too small, delete them.
641
 * On entry, *it must be an iterator pointing to this. If this gets deleted
642
 * then this is extracted from *it, so an iteration can continue.
643
 * @param min_size minimum size for outline
644
 * @param it outline iterator
645
 */
646
2.39M
void C_OUTLINE::RemoveSmallRecursive(int min_size, C_OUTLINE_IT *it) {
647
2.39M
  if (box.width() < min_size || box.height() < min_size) {
648
359k
    ASSERT_HOST(this == it->data());
649
359k
    delete it->extract(); // Too small so get rid of it and any children.
650
2.03M
  } else if (!children.empty()) {
651
    // Search the children of this, deleting any that are too small.
652
66.9k
    C_OUTLINE_IT child_it(&children);
653
226k
    for (child_it.mark_cycle_pt(); !child_it.cycled_list(); child_it.forward()) {
654
159k
      C_OUTLINE *child = child_it.data();
655
159k
      child->RemoveSmallRecursive(min_size, &child_it);
656
159k
    }
657
66.9k
  }
658
2.39M
}
659
660
// Factored out helpers below are used only by ComputeEdgeOffsets to operate
661
// on data from an 8-bit Pix, and assume that any input x and/or y are already
662
// constrained to be legal Pix coordinates.
663
664
/**
665
 * Helper computes the local 2-D gradient (dx, dy) from the 2x2 cell centered
666
 * on the given (x,y). If the cell would go outside the image, it is padded
667
 * with white.
668
 */
669
static void ComputeGradient(const l_uint32 *data, int wpl, int x, int y, int width, int height,
670
0
                            ICOORD *gradient) {
671
0
  const l_uint32 *line = data + y * wpl;
672
0
  int pix_x_y = x < width && y < height ? GET_DATA_BYTE(line, x) : 255;
673
0
  int pix_x_prevy = x < width && y > 0 ? GET_DATA_BYTE(line - wpl, x) : 255;
674
0
  int pix_prevx_prevy = x > 0 && y > 0 ? GET_DATA_BYTE(line - wpl, x - 1) : 255;
675
0
  int pix_prevx_y = x > 0 && y < height ? GET_DATA_BYTE(line, x - 1) : 255;
676
0
  gradient->set_x(pix_x_y + pix_x_prevy - (pix_prevx_y + pix_prevx_prevy));
677
0
  gradient->set_y(pix_x_prevy + pix_prevx_prevy - (pix_x_y + pix_prevx_y));
678
0
}
679
680
/**
681
 * Helper evaluates a vertical difference, (x,y) - (x,y-1), returning true if
682
 * the difference, matches diff_sign and updating the best_diff, best_sum,
683
 * best_y if a new max.
684
 */
685
static bool EvaluateVerticalDiff(const l_uint32 *data, int wpl, int diff_sign, int x, int y,
686
0
                                 int height, int *best_diff, int *best_sum, int *best_y) {
687
0
  if (y <= 0 || y >= height) {
688
0
    return false;
689
0
  }
690
0
  const l_uint32 *line = data + y * wpl;
691
0
  int pixel1 = GET_DATA_BYTE(line - wpl, x);
692
0
  int pixel2 = GET_DATA_BYTE(line, x);
693
0
  int diff = (pixel2 - pixel1) * diff_sign;
694
0
  if (diff > *best_diff) {
695
0
    *best_diff = diff;
696
0
    *best_sum = pixel1 + pixel2;
697
0
    *best_y = y;
698
0
  }
699
0
  return diff > 0;
700
0
}
701
702
/**
703
 * Helper evaluates a horizontal difference, (x,y) - (x-1,y), where y is implied
704
 * by the input image line, returning true if the difference matches diff_sign
705
 * and updating the best_diff, best_sum, best_x if a new max.
706
 */
707
static bool EvaluateHorizontalDiff(const l_uint32 *line, int diff_sign, int x, int width,
708
0
                                   int *best_diff, int *best_sum, int *best_x) {
709
0
  if (x <= 0 || x >= width) {
710
0
    return false;
711
0
  }
712
0
  int pixel1 = GET_DATA_BYTE(line, x - 1);
713
0
  int pixel2 = GET_DATA_BYTE(line, x);
714
0
  int diff = (pixel2 - pixel1) * diff_sign;
715
0
  if (diff > *best_diff) {
716
0
    *best_diff = diff;
717
0
    *best_sum = pixel1 + pixel2;
718
0
    *best_x = x;
719
0
  }
720
0
  return diff > 0;
721
0
}
722
723
/**
724
 * Adds sub-pixel resolution EdgeOffsets for the outline if the supplied
725
 * pix is 8-bit. Does nothing otherwise.
726
 * Operation: Consider the following near-horizontal line:
727
 * @verbatim
728
 *   _________
729
 *            |________
730
 *                     |________
731
 * @endverbatim
732
 * At *every* position along this line, the gradient direction will be close
733
 * to vertical. Extrapoaltion/interpolation of the position of the threshold
734
 * that was used to binarize the image gives a more precise vertical position
735
 * for each horizontal step, and the conflict in step direction and gradient
736
 * direction can be used to ignore the vertical steps.
737
 */
738
0
void C_OUTLINE::ComputeEdgeOffsets(int threshold, Image pix) {
739
0
  if (pixGetDepth(pix) != 8) {
740
0
    return;
741
0
  }
742
0
  const l_uint32 *data = pixGetData(pix);
743
0
  int wpl = pixGetWpl(pix);
744
0
  int width = pixGetWidth(pix);
745
0
  int height = pixGetHeight(pix);
746
0
  bool negative = flag(COUT_INVERSE);
747
0
  delete[] offsets;
748
0
  offsets = new EdgeOffset[stepcount];
749
0
  ICOORD pos = start;
750
0
  ICOORD prev_gradient;
751
0
  ComputeGradient(data, wpl, pos.x(), height - pos.y(), width, height, &prev_gradient);
752
0
  for (int s = 0; s < stepcount; ++s) {
753
0
    ICOORD step_vec = step(s);
754
0
    TPOINT pt1(pos);
755
0
    pos += step_vec;
756
0
    TPOINT pt2(pos);
757
0
    ICOORD next_gradient;
758
0
    ComputeGradient(data, wpl, pos.x(), height - pos.y(), width, height, &next_gradient);
759
    // Use the sum of the prev and next as the working gradient.
760
0
    ICOORD gradient = prev_gradient + next_gradient;
761
    // best_diff will be manipulated to be always positive.
762
0
    int best_diff = 0;
763
    // offset will be the extrapolation of the location of the greyscale
764
    // threshold from the edge with the largest difference, relative to the
765
    // location of the binary edge.
766
0
    int offset = 0;
767
0
    if (pt1.y == pt2.y && abs(gradient.y()) * 2 >= abs(gradient.x())) {
768
      // Horizontal step. diff_sign == 1 indicates black above.
769
0
      int diff_sign = (pt1.x > pt2.x) == negative ? 1 : -1;
770
0
      int x = std::min(pt1.x, pt2.x);
771
0
      int y = height - pt1.y;
772
0
      int best_sum = 0;
773
0
      int best_y = y;
774
0
      EvaluateVerticalDiff(data, wpl, diff_sign, x, y, height, &best_diff, &best_sum, &best_y);
775
      // Find the strongest edge.
776
0
      int test_y = y;
777
0
      do {
778
0
        ++test_y;
779
0
      } while (EvaluateVerticalDiff(data, wpl, diff_sign, x, test_y, height, &best_diff, &best_sum,
780
0
                                    &best_y));
781
0
      test_y = y;
782
0
      do {
783
0
        --test_y;
784
0
      } while (EvaluateVerticalDiff(data, wpl, diff_sign, x, test_y, height, &best_diff, &best_sum,
785
0
                                    &best_y));
786
0
      offset = diff_sign * (best_sum / 2 - threshold) + (y - best_y) * best_diff;
787
0
    } else if (pt1.x == pt2.x && abs(gradient.x()) * 2 >= abs(gradient.y())) {
788
      // Vertical step. diff_sign == 1 indicates black on the left.
789
0
      int diff_sign = (pt1.y > pt2.y) == negative ? 1 : -1;
790
0
      int x = pt1.x;
791
0
      int y = height - std::max(pt1.y, pt2.y);
792
0
      const l_uint32 *line = pixGetData(pix) + y * wpl;
793
0
      int best_sum = 0;
794
0
      int best_x = x;
795
0
      EvaluateHorizontalDiff(line, diff_sign, x, width, &best_diff, &best_sum, &best_x);
796
      // Find the strongest edge.
797
0
      int test_x = x;
798
0
      do {
799
0
        ++test_x;
800
0
      } while (
801
0
          EvaluateHorizontalDiff(line, diff_sign, test_x, width, &best_diff, &best_sum, &best_x));
802
0
      test_x = x;
803
0
      do {
804
0
        --test_x;
805
0
      } while (
806
0
          EvaluateHorizontalDiff(line, diff_sign, test_x, width, &best_diff, &best_sum, &best_x));
807
0
      offset = diff_sign * (threshold - best_sum / 2) + (best_x - x) * best_diff;
808
0
    }
809
0
    offsets[s].offset_numerator = ClipToRange<int>(offset, -INT8_MAX, INT8_MAX);
810
0
    offsets[s].pixel_diff = ClipToRange<int>(best_diff, 0, UINT8_MAX);
811
0
    if (negative) {
812
0
      gradient = -gradient;
813
0
    }
814
    // Compute gradient angle quantized to 256 directions, rotated by 64 (pi/2)
815
    // to convert from gradient direction to edge direction.
816
0
    offsets[s].direction = Modulo(FCOORD::binary_angle_plus_pi(gradient.angle()) + 64, 256);
817
0
    prev_gradient = next_gradient;
818
0
  }
819
0
}
820
821
/**
822
 * Adds sub-pixel resolution EdgeOffsets for the outline using only
823
 * a binary image source.
824
 *
825
 * Runs a sliding window of 5 edge steps over the outline, maintaining a count
826
 * of the number of steps in each of the 4 directions in the window, and a
827
 * sum of the x or y position of each step (as appropriate to its direction.)
828
 * Ignores single-count steps EXCEPT the sharp U-turn and smoothes out the
829
 * perpendicular direction. Eg
830
 * @verbatim
831
 * ___              ___       Chain code from the left:
832
 *    |___    ___   ___|      222122212223221232223000
833
 *        |___|  |_|          Corresponding counts of each direction:
834
 *                          0   00000000000000000123
835
 *                          1   11121111001111100000
836
 *                          2   44434443443333343321
837
 *                          3   00000001111111112111
838
 * Count of direction at center 41434143413313143313
839
 * Step gets used?              YNYYYNYYYNYYNYNYYYyY (y= U-turn exception)
840
 * Path redrawn showing only the used points:
841
 * ___              ___
842
 *     ___    ___   ___|
843
 *         ___    _
844
 * @endverbatim
845
 * Sub-pixel edge position cannot be shown well with ASCII-art, but each
846
 * horizontal step's y position is the mean of the y positions of the steps
847
 * in the same direction in the sliding window, which makes a much smoother
848
 * outline, without losing important detail.
849
 */
850
2.71M
void C_OUTLINE::ComputeBinaryOffsets() {
851
2.71M
  delete[] offsets;
852
2.71M
  offsets = new EdgeOffset[stepcount];
853
  // Count of the number of steps in each direction in the sliding window.
854
2.71M
  int dir_counts[4];
855
  // Sum of the positions (y for a horizontal step, x for vertical) in each
856
  // direction in the sliding window.
857
2.71M
  int pos_totals[4];
858
2.71M
  memset(dir_counts, 0, sizeof(dir_counts));
859
2.71M
  memset(pos_totals, 0, sizeof(pos_totals));
860
2.71M
  ICOORD pos = start;
861
2.71M
  ICOORD tail_pos = pos;
862
  // tail_pos is the trailing position, with the next point to be lost from
863
  // the window.
864
2.71M
  tail_pos -= step(stepcount - 1);
865
2.71M
  tail_pos -= step(stepcount - 2);
866
  // head_pos is the leading position, with the next point to be added to the
867
  // window.
868
2.71M
  ICOORD head_pos = tail_pos;
869
  // Set up the initial window with 4 points in [-2, 2)
870
13.5M
  for (int s = -2; s < 2; ++s) {
871
10.8M
    increment_step(s, 1, &head_pos, dir_counts, pos_totals);
872
10.8M
  }
873
71.6M
  for (int s = 0; s < stepcount; pos += step(s++)) {
874
    // At step s, s in the middle of [s-2, s+2].
875
68.9M
    increment_step(s + 2, 1, &head_pos, dir_counts, pos_totals);
876
68.9M
    int dir_index = chain_code(s);
877
68.9M
    ICOORD step_vec = step(s);
878
68.9M
    int best_diff = 0;
879
68.9M
    int offset = 0;
880
    // Use only steps that have a count of >=2 OR the strong U-turn with a
881
    // single d and 2 at d-1 and 2 at d+1 (mod 4).
882
68.9M
    if (dir_counts[dir_index] >= 2 ||
883
10.0M
        (dir_counts[dir_index] == 1 && dir_counts[Modulo(dir_index - 1, 4)] == 2 &&
884
62.9M
         dir_counts[Modulo(dir_index + 1, 4)] == 2)) {
885
      // Valid step direction.
886
62.9M
      best_diff = dir_counts[dir_index];
887
62.9M
      int edge_pos = step_vec.x() == 0 ? pos.x() : pos.y();
888
      // The offset proposes that the actual step should be positioned at
889
      // the mean position of the steps in the window of the same direction.
890
      // See ASCII art above.
891
62.9M
      offset = pos_totals[dir_index] - best_diff * edge_pos;
892
62.9M
    }
893
68.9M
    offsets[s].offset_numerator = ClipToRange<int>(offset, -INT8_MAX, INT8_MAX);
894
68.9M
    offsets[s].pixel_diff = ClipToRange<int>(best_diff, 0, UINT8_MAX);
895
    // The direction is just the vector from start to end of the window.
896
68.9M
    FCOORD direction(head_pos.x() - tail_pos.x(), head_pos.y() - tail_pos.y());
897
68.9M
    offsets[s].direction = direction.to_direction();
898
68.9M
    increment_step(s - 2, -1, &tail_pos, dir_counts, pos_totals);
899
68.9M
  }
900
2.71M
}
901
902
/**
903
 * Renders the outline to the given pix, with left and top being
904
 * the coords of the upper-left corner of the pix.
905
 */
906
0
void C_OUTLINE::render(int left, int top, Image pix) const {
907
0
  ICOORD pos = start;
908
0
  for (int stepindex = 0; stepindex < stepcount; ++stepindex) {
909
0
    ICOORD next_step = step(stepindex);
910
0
    if (next_step.y() < 0) {
911
0
      pixRasterop(pix, 0, top - pos.y(), pos.x() - left, 1, PIX_NOT(PIX_DST), nullptr, 0, 0);
912
0
    } else if (next_step.y() > 0) {
913
0
      pixRasterop(pix, 0, top - pos.y() - 1, pos.x() - left, 1, PIX_NOT(PIX_DST), nullptr, 0, 0);
914
0
    }
915
0
    pos += next_step;
916
0
  }
917
0
}
918
919
/**
920
 * Renders just the outline to the given pix (no fill), with left and top
921
 * being the coords of the upper-left corner of the pix.
922
 * @param left coord
923
 * @param top coord
924
 * @param pix the pix to outline
925
 */
926
0
void C_OUTLINE::render_outline(int left, int top, Image pix) const {
927
0
  ICOORD pos = start;
928
0
  for (int stepindex = 0; stepindex < stepcount; ++stepindex) {
929
0
    ICOORD next_step = step(stepindex);
930
0
    if (next_step.y() < 0) {
931
0
      pixSetPixel(pix, pos.x() - left, top - pos.y(), 1);
932
0
    } else if (next_step.y() > 0) {
933
0
      pixSetPixel(pix, pos.x() - left - 1, top - pos.y() - 1, 1);
934
0
    } else if (next_step.x() < 0) {
935
0
      pixSetPixel(pix, pos.x() - left - 1, top - pos.y(), 1);
936
0
    } else if (next_step.x() > 0) {
937
0
      pixSetPixel(pix, pos.x() - left, top - pos.y() - 1, 1);
938
0
    }
939
0
    pos += next_step;
940
0
  }
941
0
}
942
943
/**
944
 * @name C_OUTLINE::plot
945
 *
946
 * Draw the outline in the given colour.
947
 * @param window window to draw in
948
 * @param colour colour to draw in
949
 */
950
951
#ifndef GRAPHICS_DISABLED
952
void C_OUTLINE::plot(ScrollView *window, ScrollView::Color colour) const {
953
  int16_t stepindex; // index to cstep
954
  ICOORD pos;        // current position
955
  DIR128 stepdir;    // direction of step
956
957
  pos = start; // current position
958
  window->Pen(colour);
959
  if (stepcount == 0) {
960
    window->Rectangle(box.left(), box.top(), box.right(), box.bottom());
961
    return;
962
  }
963
  window->SetCursor(pos.x(), pos.y());
964
965
  stepindex = 0;
966
  while (stepindex < stepcount) {
967
    pos += step(stepindex); // step to next
968
    stepdir = step_dir(stepindex);
969
    stepindex++; // count steps
970
    // merge straight lines
971
    while (stepindex < stepcount && stepdir.get_dir() == step_dir(stepindex).get_dir()) {
972
      pos += step(stepindex);
973
      stepindex++;
974
    }
975
    window->DrawTo(pos.x(), pos.y());
976
  }
977
}
978
979
/**
980
 * Draws the outline in the given colour, normalized using the given denorm,
981
 * making use of sub-pixel accurate information if available.
982
 */
983
void C_OUTLINE::plot_normed(const DENORM &denorm, ScrollView::Color colour,
984
                            ScrollView *window) const {
985
  window->Pen(colour);
986
  if (stepcount == 0) {
987
    window->Rectangle(box.left(), box.top(), box.right(), box.bottom());
988
    return;
989
  }
990
  const DENORM *root_denorm = denorm.RootDenorm();
991
  ICOORD pos = start; // current position
992
  FCOORD f_pos = sub_pixel_pos_at_index(pos, 0);
993
  FCOORD pos_normed;
994
  denorm.NormTransform(root_denorm, f_pos, &pos_normed);
995
  window->SetCursor(IntCastRounded(pos_normed.x()), IntCastRounded(pos_normed.y()));
996
  for (int s = 0; s < stepcount; pos += step(s++)) {
997
    int edge_weight = edge_strength_at_index(s);
998
    if (edge_weight == 0) {
999
      // This point has conflicting gradient and step direction, so ignore it.
1000
      continue;
1001
    }
1002
    FCOORD f_pos = sub_pixel_pos_at_index(pos, s);
1003
    FCOORD pos_normed;
1004
    denorm.NormTransform(root_denorm, f_pos, &pos_normed);
1005
    window->DrawTo(IntCastRounded(pos_normed.x()), IntCastRounded(pos_normed.y()));
1006
  }
1007
}
1008
#endif
1009
1010
/**
1011
 * @name C_OUTLINE::operator=
1012
 *
1013
 * Assignment - deep copy data
1014
 * @param source assign from this
1015
 */
1016
1017
781k
C_OUTLINE &C_OUTLINE::operator=(const C_OUTLINE &source) {
1018
781k
  box = source.box;
1019
781k
  start = source.start;
1020
781k
  if (!children.empty()) {
1021
0
    children.clear();
1022
0
  }
1023
781k
  children.deep_copy(&source.children, &deep_copy);
1024
781k
  delete[] offsets;
1025
781k
  offsets = nullptr;
1026
781k
  stepcount = source.stepcount;
1027
781k
  if (stepcount > 0) {
1028
781k
    steps.resize(step_mem());
1029
781k
    memmove(&steps[0], &source.steps[0], step_mem());
1030
781k
    if (source.offsets != nullptr) {
1031
746k
      offsets = new EdgeOffset[stepcount];
1032
746k
      memcpy(offsets, source.offsets, stepcount * sizeof(*offsets));
1033
746k
    }
1034
781k
  }
1035
781k
  return *this;
1036
781k
}
1037
1038
/**
1039
 * Helper for ComputeBinaryOffsets. Increments pos, dir_counts, pos_totals
1040
 * by the step, increment, and vertical step ? x : y position * increment
1041
 * at step s Mod stepcount respectively. Used to add or subtract the
1042
 * direction and position to/from accumulators of a small neighbourhood.
1043
 */
1044
void C_OUTLINE::increment_step(int s, int increment, ICOORD *pos, int *dir_counts,
1045
148M
                               int *pos_totals) const {
1046
148M
  int step_index = Modulo(s, stepcount);
1047
148M
  int dir_index = chain_code(step_index);
1048
148M
  dir_counts[dir_index] += increment;
1049
148M
  ICOORD step_vec = step(step_index);
1050
148M
  if (step_vec.x() == 0) {
1051
85.5M
    pos_totals[dir_index] += pos->x() * increment;
1052
85.5M
  } else {
1053
63.2M
    pos_totals[dir_index] += pos->y() * increment;
1054
63.2M
  }
1055
148M
  *pos += step_vec;
1056
148M
}
1057
1058
0
ICOORD C_OUTLINE::chain_step(int chaindir) {
1059
0
  return step_coords[chaindir % 4];
1060
0
}
1061
1062
} // namespace tesseract