516 inT16 projection_offset;
520 inT16 best_left_x = 0;
521 inT16 best_right_x = 0;
532 FPSEGPT_IT seg_it = seg_list;
534 end = (end - start) % pitch;
537 if ((pitch - 3) / 2 < pitch_error)
538 pitch_error = (pitch - 3) / 2;
541 for (left_edge = projection_left; projection->
pile_count (left_edge) == 0
542 && left_edge < projection_right; left_edge++);
543 for (right_edge = projection_right; projection->
pile_count (right_edge) == 0
544 && right_edge > left_edge; right_edge--);
545 array_origin = left_edge - pitch;
549 for (x = array_origin; x < left_edge; x++)
551 cutpts[x - array_origin].setup (cutpts, array_origin, projection, zero_count, pitch, x, 0);
552 prev_zero = left_edge - 1;
553 for (offset = 0; offset <= pitch_error; offset++, x++)
555 cutpts[x - array_origin].setup (cutpts, array_origin, projection, zero_count, pitch, x, offset);
559 for (offset = -pitch_error, minindex = 0; offset < pitch_error;
560 offset++, minindex++)
561 mins[minindex] = projection->
local_min (x + offset);
562 next_zero = x + zero_offset + 1;
563 for (offset = next_zero - 1; offset >= x; offset--) {
564 if (projection->
pile_count (offset) <= zero_count) {
569 while (x < right_edge - pitch_error) {
570 mins[minindex] = projection->
local_min (x + pitch_error);
572 if (minindex > pitch_error * 2)
577 if (projection->
pile_count (x) <= zero_count) {
581 for (offset = 1; offset <= pitch_error; offset++)
582 if (projection->
pile_count (x + offset) <= zero_count
583 || projection->
pile_count (x - offset) <= zero_count)
586 if (offset > pitch_error) {
587 if (x - prev_zero > zero_offset && next_zero - x > zero_offset) {
588 for (offset = 0; offset <= pitch_error; offset++) {
589 test_index = minindex + pitch_error + offset;
590 if (test_index > pitch_error * 2)
591 test_index -= pitch_error * 2 + 1;
592 if (mins[test_index])
594 test_index = minindex + pitch_error - offset;
595 if (test_index > pitch_error * 2)
596 test_index -= pitch_error * 2 + 1;
597 if (mins[test_index])
601 if (offset > pitch_error) {
608 if (projection_offset > offset)
609 offset = projection_offset;
613 if ((start == 0 && end == 0)
615 || (x - projection_left - start) % pitch <= end)
616 cutpts[x - array_origin].
assign (cutpts, array_origin, x,
617 faking, mid_cut, offset, projection,
618 projection_scale, zero_count, pitch,
621 cutpts[x - array_origin].
assign_cheap (cutpts, array_origin, x,
622 faking, mid_cut, offset,
623 projection, projection_scale,
627 if (next_zero < x || next_zero == x + zero_offset)
628 next_zero = x + zero_offset + 1;
629 if (projection->
pile_count (x + zero_offset) <= zero_count)
630 next_zero = x + zero_offset;
636 while (x < right_edge + pitch) {
637 offset = x < right_edge ? right_edge - x : 0;
638 cutpts[x - array_origin].
assign (cutpts, array_origin, x,
640 projection_scale, zero_count, pitch,
643 if (cutpts[x - array_origin].index () +
644 cutpts[x - array_origin].fake_count <= best_count + best_fake) {
645 if (cutpts[x - array_origin].fake_count < best_fake
646 || (cutpts[x - array_origin].fake_count == best_fake
647 && cutpts[x - array_origin].cost_function () < best_cost)) {
648 best_fake = cutpts[x - array_origin].
fake_count;
652 best_count = cutpts[x - array_origin].
index ();
654 else if (cutpts[x - array_origin].fake_count == best_fake
655 && x == best_right_x + 1
656 && cutpts[x - array_origin].cost_function () == best_cost) {
665 best_end = &cutpts[(best_left_x + best_right_x) / 2 - array_origin];
674 occupation_count = -1;
676 for (x = best_end->
position () - pitch + pitch_error;
677 x < best_end->
position () - pitch_error
679 if (x < best_end->position () - pitch_error)
682 segpt =
new FPSEGPT (best_end);
683 seg_it.add_before_then_move (segpt);
686 while (best_end !=
NULL);
687 seg_it.move_to_last ();
688 mean_sum = seg_it.data ()->
sum ();
689 mean_sum = mean_sum * mean_sum / best_count;
690 if (seg_it.data ()->squares () - mean_sum < 0)
691 tprintf (
"Impossible sqsum=%g, mean=%g, total=%d\n",
692 seg_it.data ()->squares (), seg_it.data ()->sum (), best_count);
695 return seg_it.data ()->squares () - mean_sum;
void free_mem(void *oldchunk)
void * alloc_mem(inT32 count)
bool local_min(inT32 x) const
inT32 pile_count(inT32 value) const
void assign(FPCUTPT cutpts[], inT16 array_origin, inT16 x, BOOL8 faking, BOOL8 mid_cut, inT16 offset, STATS *projection, float projection_scale, inT16 zero_count, inT16 pitch, inT16 pitch_error)
void assign_cheap(FPCUTPT cutpts[], inT16 array_origin, inT16 x, BOOL8 faking, BOOL8 mid_cut, inT16 offset, STATS *projection, float projection_scale, inT16 zero_count, inT16 pitch, inT16 pitch_error)
EXTERN bool textord_fast_pitch_test
DLLSYM void tprintf(const char *format,...)
EXTERN double pitsync_joined_edge