27 #include "allheaders.h" 37 ICOORD C_OUTLINE::step_coords[4] = {
53 : box(bot_left, top_right), start(startpt->pos), offsets(NULL) {
64 memset(steps, 0, step_mem());
67 for (stepindex = 0; stepindex < length; stepindex++) {
70 edgept = edgept->
next;
84 ):start (startpt), offsets(NULL) {
98 memset(steps, 0, step_mem());
100 lastdir = new_steps[length - 1];
102 for (stepindex = 0, srcindex = 0; srcindex < length;
103 stepindex++, srcindex++) {
104 new_box =
TBOX (pos, pos);
107 dir = new_steps[srcindex];
109 dirdiff = dir - prevdir;
110 pos +=
step (stepindex);
111 if ((dirdiff == 64 || dirdiff == -64) && stepindex > 0) {
113 prevdir = stepindex >= 0 ?
step_dir (stepindex) : lastdir;
121 if (dirdiff == 64 || dirdiff == -64) {
124 for (
int i = 0; i < stepindex; ++i)
128 while (stepindex > 1 && (dirdiff == 64 || dirdiff == -64));
129 stepcount = stepindex;
153 stepcount = srcline->stepcount * 2;
154 if (stepcount == 0) {
162 memset(steps, 0, step_mem());
164 for (
int iteration = 0; iteration < 2; ++iteration) {
165 DIR128 round1 = iteration == 0 ? 32 : 0;
166 DIR128 round2 = iteration != 0 ? 32 : 0;
167 pos = srcline->start;
169 prevpos.
rotate (rotation);
171 box =
TBOX (start, start);
173 for (stepindex = 0; stepindex < srcline->stepcount; stepindex++) {
174 pos += srcline->
step (stepindex);
176 destpos.
rotate (rotation);
178 while (destpos.
x () != prevpos.
x () || destpos.
y () != prevpos.
y ()) {
184 set_step(destindex++, dir + round1);
185 prevpos +=
step(destindex - 1);
189 -64 && dirdiff != 64)) {
190 set_step(destindex++, dir + round2);
191 prevpos +=
step(destindex - 1);
193 prevpos -=
step(destindex - 1);
195 prevpos -=
step(destindex - 1);
196 set_step(destindex - 1, dir + round2);
197 prevpos +=
step(destindex - 1);
202 prevpos +=
step(destindex - 1);
204 while (destindex >= 2 &&
208 prevpos -=
step(destindex - 1);
209 prevpos -=
step(destindex - 2);
213 new_box =
TBOX (destpos, destpos);
217 ASSERT_HOST (destpos.
x () == start.
x () && destpos.
y () == start.
y ());
219 while ((dirdiff == 64 || dirdiff == -64) && destindex > 1) {
222 for (
int i = 0; i < destindex; ++i)
230 stepcount = destindex;
232 for (stepindex = 0; stepindex < stepcount; stepindex++) {
233 destpos +=
step (stepindex);
235 ASSERT_HOST (destpos.
x () == start.
x () && destpos.
y () == start.
y ());
240 C_OUTLINE_IT ol_it(outlines);
246 ol_it.add_to_end(outline);
263 C_OUTLINE_IT it(const_cast<C_OUTLINE_LIST*>(&children));
268 for (stepindex = 0; stepindex < total_steps; stepindex++) {
270 next_step =
step (stepindex);
271 if (next_step.
x () < 0)
273 else if (next_step.
x () > 0)
277 for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ())
278 total += it.data ()->area ();
293 C_OUTLINE_IT it(const_cast<C_OUTLINE_LIST*>(&children));
296 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward())
297 total_steps += it.data()->pathlength();
317 if (total_steps == 0)
320 for (stepindex = 0; stepindex < total_steps; stepindex++) {
322 next_step =
step (stepindex);
323 if (next_step.
x () < 0)
325 else if (next_step.
x () > 0)
341 BOOL8 first_was_max_x;
342 BOOL8 first_was_max_y;
343 BOOL8 looking_for_max_x;
344 BOOL8 looking_for_min_x;
345 BOOL8 looking_for_max_y;
346 BOOL8 looking_for_min_y;
350 inT32 max_x, min_x, max_y, min_y;
351 inT32 initial_x, initial_y;
359 max_x = min_x = pos.
x ();
360 max_y = min_y = pos.
y ();
361 looking_for_max_x =
TRUE;
362 looking_for_min_x =
TRUE;
363 looking_for_max_y =
TRUE;
364 looking_for_min_y =
TRUE;
365 first_was_max_x =
FALSE;
366 first_was_max_y =
FALSE;
367 initial_x = pos.
x ();
368 initial_y = pos.
y ();
369 for (stepindex = 0; stepindex < total_steps; stepindex++) {
371 next_step =
step (stepindex);
373 if (next_step.
x () < 0) {
374 if (looking_for_max_x && pos.
x () < min_x)
376 if (looking_for_min_x && max_x - pos.
x () > threshold) {
377 if (looking_for_max_x) {
379 first_was_max_x =
FALSE;
382 looking_for_max_x =
TRUE;
383 looking_for_min_x =
FALSE;
387 else if (next_step.
x () > 0) {
388 if (looking_for_min_x && pos.
x () > max_x)
390 if (looking_for_max_x && pos.
x () - min_x > threshold) {
391 if (looking_for_min_x) {
393 first_was_max_x =
TRUE;
396 looking_for_max_x =
FALSE;
397 looking_for_min_x =
TRUE;
401 else if (next_step.
y () < 0) {
402 if (looking_for_max_y && pos.
y () < min_y)
404 if (looking_for_min_y && max_y - pos.
y () > threshold) {
405 if (looking_for_max_y) {
407 first_was_max_y =
FALSE;
410 looking_for_max_y =
TRUE;
411 looking_for_min_y =
FALSE;
416 if (looking_for_min_y && pos.
y () > max_y)
418 if (looking_for_max_y && pos.
y () - min_y > threshold) {
419 if (looking_for_min_y) {
421 first_was_max_y =
TRUE;
424 looking_for_max_y =
FALSE;
425 looking_for_min_y =
TRUE;
431 if (first_was_max_x && looking_for_min_x) {
432 if (max_x - initial_x > threshold)
437 else if (!first_was_max_x && looking_for_max_x) {
438 if (initial_x - min_x > threshold)
443 if (first_was_max_y && looking_for_min_y) {
444 if (max_y - initial_y > threshold)
449 else if (!first_was_max_y && looking_for_max_y) {
450 if (initial_y - min_y > threshold)
475 return other.box.
contains(this->box);
478 for (stepindex = 0; stepindex < stepcount
480 pos +=
step (stepindex);
484 for (stepindex = 0; stepindex < other.stepcount
487 pos += other.
step (stepindex);
509 for (stepindex = 0; stepindex < stepcount; stepindex++) {
510 stepvec =
step (stepindex);
512 if (vec.
y () <= 0 && vec.
y () + stepvec.
y () > 0) {
513 cross = vec * stepvec;
519 else if (vec.
y () > 0 && vec.
y () + stepvec.
y () <= 0) {
520 cross = vec * stepvec;
548 for (stepindex = 0; stepindex < stepcount; stepindex++) {
550 dirdiff = dir - prevdir;
551 ASSERT_HOST (dirdiff == 0 || dirdiff == 32 || dirdiff == -32);
572 halfsteps = (stepcount + 1) / 2;
573 for (stepindex = 0; stepindex < halfsteps; stepindex++) {
574 farindex = stepcount - stepindex - 1;
577 set_step (farindex, stepdir + halfturn);
589 C_OUTLINE_IT it(&children);
594 for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ())
595 it.data ()->move (vec);
605 if (stepcount == 0)
return true;
609 C_OUTLINE_IT child_it(const_cast<C_OUTLINE_LIST*>(&children));
610 for (child_it.mark_cycle_pt(); !child_it.cycled_list(); child_it.forward()) {
612 if (
child->outer_area() * parent_area > 0 || !
child->IsLegallyNested())
628 if (box.
width() < min_size || box.
height() < min_size) {
630 delete it->extract();
631 }
else if (!children.empty()) {
633 C_OUTLINE_IT child_it(&children);
634 for (child_it.mark_cycle_pt(); !child_it.cycled_list();
635 child_it.forward()) {
637 child->RemoveSmallRecursive(min_size, &child_it);
651 static void ComputeGradient(
const l_uint32* data,
int wpl,
652 int x,
int y,
int width,
int height,
654 const l_uint32* line = data + y * wpl;
656 x < width && y < height
658 const_cast<void*>(reinterpret_cast<const void*>(line)), x)
663 const_cast<void*>(reinterpret_cast<const void*>(line - wpl)), x)
665 int pix_prevx_prevy =
668 const_cast<void*>(reinterpret_cast<void const*>(line - wpl)),
674 const_cast<void*>(reinterpret_cast<const void*>(line)), x - 1)
676 gradient->
set_x(pix_x_y + pix_x_prevy - (pix_prevx_y + pix_prevx_prevy));
677 gradient->
set_y(pix_x_prevy + pix_prevx_prevy - (pix_x_y + pix_prevx_y));
685 static bool EvaluateVerticalDiff(
const l_uint32* data,
int wpl,
int diff_sign,
686 int x,
int y,
int height,
687 int* best_diff,
int* best_sum,
int* best_y) {
688 if (y <= 0 || y >= height)
690 const l_uint32* line = data + y * wpl;
691 int pixel1 = GET_DATA_BYTE(
692 const_cast<void*>(reinterpret_cast<const void*>(line - wpl)), x);
694 GET_DATA_BYTE(const_cast<void*>(reinterpret_cast<const void*>(line)), x);
695 int diff = (pixel2 - pixel1) * diff_sign;
696 if (diff > *best_diff) {
698 *best_sum = pixel1 + pixel2;
709 static bool EvaluateHorizontalDiff(
const l_uint32* line,
int diff_sign,
711 int* best_diff,
int* best_sum,
int* best_x) {
712 if (x <= 0 || x >= width)
714 int pixel1 = GET_DATA_BYTE(
715 const_cast<void*>(reinterpret_cast<const void*>(line)), x - 1);
717 GET_DATA_BYTE(const_cast<void*>(reinterpret_cast<const void*>(line)), x);
718 int diff = (pixel2 - pixel1) * diff_sign;
719 if (diff > *best_diff) {
721 *best_sum = pixel1 + pixel2;
743 if (pixGetDepth(pix) != 8)
return;
744 const l_uint32* data = pixGetData(pix);
745 int wpl = pixGetWpl(pix);
746 int width = pixGetWidth(pix);
747 int height = pixGetHeight(pix);
753 ComputeGradient(data, wpl, pos.
x(), height - pos.
y(), width, height,
755 for (
int s = 0; s < stepcount; ++s) {
761 ComputeGradient(data, wpl, pos.
x(), height - pos.
y(), width, height,
764 ICOORD gradient = prev_gradient + next_gradient;
771 if (pt1.
y == pt2.
y && abs(gradient.y()) * 2 >= abs(gradient.x())) {
773 int diff_sign = (pt1.
x > pt2.
x) == negative ? 1 : -1;
774 int x =
MIN(pt1.
x, pt2.
x);
775 int y = height - pt1.
y;
778 EvaluateVerticalDiff(data, wpl, diff_sign, x, y, height,
779 &best_diff, &best_sum, &best_y);
784 }
while (EvaluateVerticalDiff(data, wpl, diff_sign, x, test_y, height,
785 &best_diff, &best_sum, &best_y));
789 }
while (EvaluateVerticalDiff(data, wpl, diff_sign, x, test_y, height,
790 &best_diff, &best_sum, &best_y));
791 offset = diff_sign * (best_sum / 2 - threshold) +
792 (y - best_y) * best_diff;
793 }
else if (pt1.
x == pt2.
x && abs(gradient.x()) * 2 >= abs(gradient.y())) {
795 int diff_sign = (pt1.
y > pt2.
y) == negative ? 1 : -1;
797 int y = height -
MAX(pt1.
y, pt2.
y);
798 const l_uint32* line = pixGetData(pix) + y * wpl;
801 EvaluateHorizontalDiff(line, diff_sign, x, width,
802 &best_diff, &best_sum, &best_x);
807 }
while (EvaluateHorizontalDiff(line, diff_sign, test_x, width,
808 &best_diff, &best_sum, &best_x));
812 }
while (EvaluateHorizontalDiff(line, diff_sign, test_x, width,
813 &best_diff, &best_sum, &best_x));
814 offset = diff_sign * (threshold - best_sum / 2) +
815 (best_x - x) * best_diff;
821 if (negative) gradient = -gradient;
826 prev_gradient = next_gradient;
867 memset(dir_counts, 0,
sizeof(dir_counts));
868 memset(pos_totals, 0,
sizeof(pos_totals));
873 tail_pos -=
step(stepcount - 1);
874 tail_pos -=
step(stepcount - 2);
877 ICOORD head_pos = tail_pos;
879 for (
int s = -2; s < 2; ++s) {
880 increment_step(s, 1, &head_pos, dir_counts, pos_totals);
882 for (
int s = 0; s < stepcount; pos +=
step(s++)) {
884 increment_step(s + 2, 1, &head_pos, dir_counts, pos_totals);
891 if (dir_counts[dir_index] >= 2 || (dir_counts[dir_index] == 1 &&
892 dir_counts[
Modulo(dir_index - 1, 4)] == 2 &&
893 dir_counts[
Modulo(dir_index + 1, 4)] == 2)) {
895 best_diff = dir_counts[dir_index];
896 int edge_pos = step_vec.
x() == 0 ? pos.
x() : pos.
y();
900 offset = pos_totals[dir_index] - best_diff * edge_pos;
909 increment_step(s - 2, -1, &tail_pos, dir_counts, pos_totals);
919 for (
int stepindex = 0; stepindex < stepcount; ++stepindex) {
921 if (next_step.
y() < 0) {
922 pixRasterop(pix, 0, top - pos.
y(), pos.
x() - left, 1,
923 PIX_NOT(PIX_DST), NULL, 0, 0);
924 }
else if (next_step.
y() > 0) {
925 pixRasterop(pix, 0, top - pos.
y() - 1, pos.
x() - left, 1,
926 PIX_NOT(PIX_DST), NULL, 0, 0);
941 for (
int stepindex = 0; stepindex < stepcount; ++stepindex) {
943 if (next_step.
y() < 0) {
944 pixSetPixel(pix, pos.
x() - left, top - pos.
y(), 1);
945 }
else if (next_step.
y() > 0) {
946 pixSetPixel(pix, pos.
x() - left - 1, top - pos.
y() - 1, 1);
947 }
else if (next_step.
x() < 0) {
948 pixSetPixel(pix, pos.
x() - left - 1, top - pos.
y(), 1);
949 }
else if (next_step.
x() > 0) {
950 pixSetPixel(pix, pos.
x() - left, top - pos.
y() - 1, 1);
964 #ifndef GRAPHICS_DISABLED 972 if (stepcount == 0) {
979 while (stepindex < stepcount) {
980 pos +=
step(stepindex);
984 while (stepindex < stepcount &&
986 pos +=
step(stepindex);
1000 if (stepcount == 0) {
1011 for (
int s = 0; s < stepcount; pos +=
step(s++)) {
1013 if (edge_weight == 0) {
1035 start = source.start;
1038 stepcount = source.stepcount;
1040 memmove (steps, source.steps, step_mem());
1041 if (!children.empty ())
1043 children.deep_copy(&source.children, &
deep_copy);
1045 if (source.offsets != NULL) {
1047 memcpy(offsets, source.offsets, stepcount *
sizeof(*offsets));
1060 void C_OUTLINE::increment_step(
int s,
int increment,
ICOORD* pos,
1061 int* dir_counts,
int* pos_totals)
const {
1062 int step_index =
Modulo(s, stepcount);
1064 dir_counts[dir_index] += increment;
1066 if (step_vec.
x() == 0)
1067 pos_totals[dir_index] += pos->
x() * increment;
1069 pos_totals[dir_index] += pos->
y() * increment;
1074 return step_coords[chaindir % 4];
#define ELISTIZE(CLASSNAME)
void DrawTo(int x, int y)
inT32 count_transitions(inT32 threshold)
void rotate(const FCOORD &vec)
static uinT8 binary_angle_plus_pi(double angle)
void render_outline(int left, int top, Pix *pix) const
inT16 winding_number(ICOORD testpt) const
bool overlap(const TBOX &box) const
BOOL8 operator<(const C_OUTLINE &other) const
int IntCastRounded(double x)
inT16 turn_direction() const
void SetCursor(int x, int y)
void render(int left, int top, Pix *pix) const
void NormTransform(const DENORM *first_norm, const TPOINT &pt, TPOINT *transformed) const
ICOORD step(int index) const
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
BOOL8 flag(C_OUTLINE_FLAGS mask) const
void set_step(inT16 stepindex, inT8 stepdir)
void move(const ICOORD vec)
int chain_code(int index) const
bool contains(const FCOORD pt) const
void RemoveSmallRecursive(int min_size, C_OUTLINE_IT *it)
C_OUTLINE & operator=(const C_OUTLINE &source)
void * alloc_mem(inT32 count)
inT16 x() const
access function
void ComputeEdgeOffsets(int threshold, Pix *pix)
bool IsLegallyNested() const
int edge_strength_at_index(int index) const
void rotate(const FCOORD &vec)
int direction(EDGEPT *point)
void plot(ScrollView *window, ScrollView::Color colour) const
void Rectangle(int x1, int y1, int x2, int y2)
static ICOORD chain_step(int chaindir)
void set_y(inT16 yin)
rewrite function
void set_x(inT16 xin)
rewrite function
const ICOORD & start_pos() const
DIR128 step_dir(int index) const
void plot_normed(const DENORM &denorm, ScrollView::Color colour, ScrollView *window) const
static C_OUTLINE * deep_copy(const C_OUTLINE *src)
static void FakeOutline(const TBOX &box, C_OUTLINE_LIST *outlines)
void free_mem(void *oldchunk)
void move(const ICOORD vec)
FCOORD sub_pixel_pos_at_index(const ICOORD &pos, int index) const
void ComputeBinaryOffsets()
const DENORM * RootDenorm() const
inT16 y() const
access_function