src/textord/textlineprojection.cpp
int TextlineProjection::VerticalDistance(bool debug, int x, int y1, int y2) const {
  x = ImageXToProjectionX(x);
  y1 = ImageYToProjectionY(y1);
  y2 = ImageYToProjectionY(y2);
  if (y1 == y2) {
    return 0;
  }
  int wpl = pixGetWpl(pix_);
  int step = y1 < y2 ? 1 : -1;
  uint32_t *data = pixGetData(pix_) + y1 * wpl;
  wpl *= step;
  int prev_pixel = GET_DATA_BYTE(data, x);
  int distance = 0;
  int right_way_steps = 0;
  for (int y = y1; y != y2; y += step) {
    data += wpl;
    int pixel = GET_DATA_BYTE(data, x);
    if (debug) {
      tprintf("At (%d,%d), pix = %d, prev=%d\n", x, y + step, pixel, prev_pixel);
    }
    if (pixel < prev_pixel) {
      distance += kWrongWayPenalty;
    } else if (pixel > prev_pixel) {
      ++right_way_steps;
    } else {
      ++distance;
    }
    prev_pixel = pixel;
  }
  return distance * scale_factor_ + right_way_steps * scale_factor_ / kWrongWayPenalty;
}
// Compute the distance between (x, y1) and (x, y2) using the rule that
// a decrease in textline density is weighted more heavily than an increase.
// The coordinates are in source image space, ie processed by any denorm
// already, but not yet scaled by scale_factor_.
// Going from the outside of a textline to the inside should measure much
// less distance than going from the inside of a textline to the outside.
// How it works:
// An increase is cheap (getting closer to a textline).
// Constant costs unity.
// A decrease is expensive (getting further from a textline).
// Pixels in projection map Counted distance
//              2
//              3              1/x
//              3               1
//              2               x
//              5              1/x
//              7              1/x
// Total: 1 + x + 3/x where x = kWrongWayPenalty.
Compute the distance between (x, y1) and (x, y2) using the rule that
a decrease in textline density is weighted more heavily than an increase.
The coordinates are in source image space, ie processed by any denorm
already, but not yet scaled by scale_factor_.
Going from the outside of a textline to the inside should measure much
less distance than going from the inside of a textline to the outside.
How it works:
An increase is cheap (getting closer to a textline).
Constant costs unity.
A decrease is expensive (getting further from a textline).
Pixels in projection map Counted distance
2
3              1/x
3               1
2               x
5              1/x
7              1/x
Total: 1 + x + 3/x where x = kWrongWayPenalty.
Compute the distance between ( x y1 ) and ( x y2 ) using the rule that a decrease in textline density is weighted more heavily than an increase . The coordinates are in source image space ie processed by any denorm already but not yet scaled by scale_factor_ . Going from the outside of a textline to the inside should measure much less distance than going from the inside of a textline to the outside . How it works : An increase is cheap ( getting closer to a textline ) . Constant costs unity . A decrease is expensive ( getting further from a textline ) . Pixels in projection map Counted distance 2 3 1 / x 3 1 2 x 5 1 / x 7 1 / x Total : 1 + x + 3 / x where x = kWrongWayPenalty .
int TextlineProjection::VerticalDistance(bool debug, int x, int y1, int y2) const {
  x = ImageXToProjectionX(x);
  y1 = ImageYToProjectionY(y1);
  y2 = ImageYToProjectionY(y2);
  if (y1 == y2) {
    return 0;
  }
  int wpl = pixGetWpl(pix_);
  int step = y1 < y2 ? 1 : -1;
  uint32_t *data = pixGetData(pix_) + y1 * wpl;
  wpl *= step;
  int prev_pixel = GET_DATA_BYTE(data, x);
  int distance = 0;
  int right_way_steps = 0;
  for (int y = y1; y != y2; y += step) {
    data += wpl;
    int pixel = GET_DATA_BYTE(data, x);
    if (debug) {
      tprintf("At (%d,%d), pix = %d, prev=%d\n", x, y + step, pixel, prev_pixel);
    }
    if (pixel < prev_pixel) {
      distance += kWrongWayPenalty;
    } else if (pixel > prev_pixel) {
      ++right_way_steps;
    } else {
      ++distance;
    }
    prev_pixel = pixel;
  }
  return distance * scale_factor_ + right_way_steps * scale_factor_ / kWrongWayPenalty;
}
int TextlineProjection :: VerticalDistance ( bool debug , int x , int y1 , int y2 ) const { x = ImageXToProjectionX ( x ) ; y1 = ImageYToProjectionY ( y1 ) ; y2 = ImageYToProjectionY ( y2 ) ; if ( y1 == y2 ) { return 0 ; } int wpl = pixGetWpl ( pix_ ) ; int step = y1 < y2 ? 1 : -1 ; uint32_t * data = pixGetData ( pix_ ) + y1 * wpl ; wpl *= step ; int prev_pixel = GET_DATA_BYTE ( data , x ) ; int distance = 0 ; int right_way_steps = 0 ; for ( int y = y1 ; y != y2 ; y += step ) { data += wpl ; int pixel = GET_DATA_BYTE ( data , x ) ; if ( debug ) { tprintf ( " \n " , x , y + step , pixel , prev_pixel ) ; } if ( pixel < prev_pixel ) { distance += kWrongWayPenalty ; } else if ( pixel > prev_pixel ) { ++ right_way_steps ; } else { ++ distance ; } prev_pixel = pixel ; } return distance * scale_factor_ + right_way_steps * scale_factor_ / kWrongWayPenalty ; }
Compute the distance between (x, y1) and (x, y2) using the rule that
a decrease in textline density is weighted more heavily than an increase.
Compute the distance between ( x y1 ) and ( x y2 ) using the rule that a decrease in textline density is weighted more heavily than an increase .