27 #include "allheaders.h"
33 #include "config_auto.h"
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;
85 ):start (startpt), offsets(
NULL) {
99 memset(steps, 0, step_mem());
101 lastdir = new_steps[length - 1];
103 for (stepindex = 0, srcindex = 0; srcindex < length;
104 stepindex++, srcindex++) {
105 new_box =
TBOX (pos, pos);
108 dir = new_steps[srcindex];
110 dirdiff = dir - prevdir;
111 pos +=
step (stepindex);
112 if ((dirdiff == 64 || dirdiff == -64) && stepindex > 0) {
114 prevdir = stepindex >= 0 ?
step_dir (stepindex) : lastdir;
122 if (dirdiff == 64 || dirdiff == -64) {
125 for (
int i = 0; i < stepindex; ++i)
129 while (stepindex > 1 && (dirdiff == 64 || dirdiff == -64));
130 stepcount = stepindex;
154 stepcount = srcline->stepcount * 2;
155 if (stepcount == 0) {
163 memset(steps, 0, step_mem());
165 for (
int iteration = 0; iteration < 2; ++iteration) {
166 DIR128 round1 = iteration == 0 ? 32 : 0;
167 DIR128 round2 = iteration != 0 ? 32 : 0;
168 pos = srcline->start;
170 prevpos.
rotate (rotation);
172 box =
TBOX (start, start);
174 for (stepindex = 0; stepindex < srcline->stepcount; stepindex++) {
175 pos += srcline->
step (stepindex);
177 destpos.
rotate (rotation);
179 while (destpos.
x () != prevpos.
x () || destpos.
y () != prevpos.
y ()) {
185 set_step(destindex++, dir + round1);
186 prevpos +=
step(destindex - 1);
190 -64 && dirdiff != 64)) {
191 set_step(destindex++, dir + round2);
192 prevpos +=
step(destindex - 1);
194 prevpos -=
step(destindex - 1);
196 prevpos -=
step(destindex - 1);
197 set_step(destindex - 1, dir + round2);
198 prevpos +=
step(destindex - 1);
203 prevpos +=
step(destindex - 1);
205 while (destindex >= 2 &&
209 prevpos -=
step(destindex - 1);
210 prevpos -=
step(destindex - 2);
214 new_box =
TBOX (destpos, destpos);
218 ASSERT_HOST (destpos.
x () == start.
x () && destpos.
y () == start.
y ());
220 while ((dirdiff == 64 || dirdiff == -64) && destindex > 1) {
223 for (
int i = 0; i < destindex; ++i)
231 stepcount = destindex;
233 for (stepindex = 0; stepindex < stepcount; stepindex++) {
234 destpos +=
step (stepindex);
236 ASSERT_HOST (destpos.
x () == start.
x () && destpos.
y () == start.
y ());
241 C_OUTLINE_IT ol_it(outlines);
247 ol_it.add_to_end(outline);
264 C_OUTLINE_IT it(const_cast<C_OUTLINE_LIST*>(&children));
269 for (stepindex = 0; stepindex < total_steps; stepindex++) {
271 next_step =
step (stepindex);
272 if (next_step.
x () < 0)
274 else if (next_step.
x () > 0)
278 for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ())
279 total += it.data ()->area ();
294 C_OUTLINE_IT it(const_cast<C_OUTLINE_LIST*>(&children));
297 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward())
298 total_steps += it.data()->pathlength();
319 if (total_steps == 0)
322 for (stepindex = 0; stepindex < total_steps; stepindex++) {
324 next_step =
step (stepindex);
325 if (next_step.
x () < 0)
327 else if (next_step.
x () > 0)
344 BOOL8 first_was_max_x;
345 BOOL8 first_was_max_y;
346 BOOL8 looking_for_max_x;
347 BOOL8 looking_for_min_x;
348 BOOL8 looking_for_max_y;
349 BOOL8 looking_for_min_y;
353 inT32 max_x, min_x, max_y, min_y;
354 inT32 initial_x, initial_y;
362 max_x = min_x = pos.
x ();
363 max_y = min_y = pos.
y ();
364 looking_for_max_x =
TRUE;
365 looking_for_min_x =
TRUE;
366 looking_for_max_y =
TRUE;
367 looking_for_min_y =
TRUE;
368 first_was_max_x =
FALSE;
369 first_was_max_y =
FALSE;
370 initial_x = pos.
x ();
371 initial_y = pos.
y ();
372 for (stepindex = 0; stepindex < total_steps; stepindex++) {
374 next_step =
step (stepindex);
376 if (next_step.
x () < 0) {
377 if (looking_for_max_x && pos.
x () < min_x)
379 if (looking_for_min_x && max_x - pos.
x () > threshold) {
380 if (looking_for_max_x) {
382 first_was_max_x =
FALSE;
385 looking_for_max_x =
TRUE;
386 looking_for_min_x =
FALSE;
390 else if (next_step.
x () > 0) {
391 if (looking_for_min_x && pos.
x () > max_x)
393 if (looking_for_max_x && pos.
x () - min_x > threshold) {
394 if (looking_for_min_x) {
396 first_was_max_x =
TRUE;
399 looking_for_max_x =
FALSE;
400 looking_for_min_x =
TRUE;
404 else if (next_step.
y () < 0) {
405 if (looking_for_max_y && pos.
y () < min_y)
407 if (looking_for_min_y && max_y - pos.
y () > threshold) {
408 if (looking_for_max_y) {
410 first_was_max_y =
FALSE;
413 looking_for_max_y =
TRUE;
414 looking_for_min_y =
FALSE;
419 if (looking_for_min_y && pos.
y () > max_y)
421 if (looking_for_max_y && pos.
y () - min_y > threshold) {
422 if (looking_for_min_y) {
424 first_was_max_y =
TRUE;
427 looking_for_max_y =
FALSE;
428 looking_for_min_y =
TRUE;
434 if (first_was_max_x && looking_for_min_x) {
435 if (max_x - initial_x > threshold)
440 else if (!first_was_max_x && looking_for_max_x) {
441 if (initial_x - min_x > threshold)
446 if (first_was_max_y && looking_for_min_y) {
447 if (max_y - initial_y > threshold)
452 else if (!first_was_max_y && looking_for_max_y) {
453 if (initial_y - min_y > threshold)
480 return other.box.
contains(this->box);
483 for (stepindex = 0; stepindex < stepcount
485 pos +=
step (stepindex);
489 for (stepindex = 0; stepindex < other.stepcount
492 pos += other.
step (stepindex);
515 for (stepindex = 0; stepindex < stepcount; stepindex++) {
516 stepvec =
step (stepindex);
518 if (vec.
y () <= 0 && vec.
y () + stepvec.
y () > 0) {
519 cross = vec * stepvec;
525 else if (vec.
y () > 0 && vec.
y () + stepvec.
y () <= 0) {
526 cross = vec * stepvec;
555 for (stepindex = 0; stepindex < stepcount; stepindex++) {
557 dirdiff = dir - prevdir;
558 ASSERT_HOST (dirdiff == 0 || dirdiff == 32 || dirdiff == -32);
580 halfsteps = (stepcount + 1) / 2;
581 for (stepindex = 0; stepindex < halfsteps; stepindex++) {
582 farindex = stepcount - stepindex - 1;
585 set_step (farindex, stepdir + halfturn);
598 C_OUTLINE_IT it(&children);
603 for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ())
604 it.data ()->move (vec);
614 if (stepcount == 0)
return true;
618 C_OUTLINE_IT child_it(const_cast<C_OUTLINE_LIST*>(&children));
619 for (child_it.mark_cycle_pt(); !child_it.cycled_list(); child_it.forward()) {
637 if (box.
width() < min_size || box.
height() < min_size) {
639 delete it->extract();
640 }
else if (!children.empty()) {
642 C_OUTLINE_IT child_it(&children);
643 for (child_it.mark_cycle_pt(); !child_it.cycled_list();
644 child_it.forward()) {
660 static void ComputeGradient(
const l_uint32* data,
int wpl,
661 int x,
int y,
int width,
int height,
663 const l_uint32* line = data + y * wpl;
664 int pix_x_y = x < width && y < height ?
665 GET_DATA_BYTE(const_cast<void*> (reinterpret_cast<const void *>(line)), x) : 255;
666 int pix_x_prevy = x < width && y > 0 ?
667 GET_DATA_BYTE(const_cast<void*> (reinterpret_cast<const void *>(line - wpl)), x) : 255;
668 int pix_prevx_prevy = x > 0 && y > 0 ?
669 GET_DATA_BYTE(const_cast<void*> (reinterpret_cast<void const*>(line - wpl)), x - 1) : 255;
670 int pix_prevx_y = x > 0 && y < height ?
671 GET_DATA_BYTE(const_cast<void*> (reinterpret_cast<const void *>(line)), x - 1) : 255;
672 gradient->
set_x(pix_x_y + pix_x_prevy - (pix_prevx_y + pix_prevx_prevy));
673 gradient->
set_y(pix_x_prevy + pix_prevx_prevy - (pix_x_y + pix_prevx_y));
681 static bool EvaluateVerticalDiff(
const l_uint32* data,
int wpl,
int diff_sign,
682 int x,
int y,
int height,
683 int* best_diff,
int* best_sum,
int* best_y) {
684 if (y <= 0 || y >= height)
686 const l_uint32* line = data + y * wpl;
687 int pixel1 = GET_DATA_BYTE(const_cast<void*> (reinterpret_cast<const void *>(line - wpl)), x);
688 int pixel2 = GET_DATA_BYTE(const_cast<void*> (reinterpret_cast<const void *>(line)), x);
689 int diff = (pixel2 - pixel1) * diff_sign;
690 if (diff > *best_diff) {
692 *best_sum = pixel1 + pixel2;
703 static bool EvaluateHorizontalDiff(
const l_uint32* line,
int diff_sign,
705 int* best_diff,
int* best_sum,
int* best_x) {
706 if (x <= 0 || x >= width)
708 int pixel1 = GET_DATA_BYTE(const_cast<void*> (reinterpret_cast<const void *>(line)), x - 1);
709 int pixel2 = GET_DATA_BYTE(const_cast<void*> (reinterpret_cast<const void *>(line)), x);
710 int diff = (pixel2 - pixel1) * diff_sign;
711 if (diff > *best_diff) {
713 *best_sum = pixel1 + pixel2;
735 if (pixGetDepth(pix) != 8)
return;
736 const l_uint32* data = pixGetData(pix);
737 int wpl = pixGetWpl(pix);
738 int width = pixGetWidth(pix);
739 int height = pixGetHeight(pix);
745 ComputeGradient(data, wpl, pos.
x(), height - pos.
y(), width, height,
747 for (
int s = 0; s < stepcount; ++s) {
753 ComputeGradient(data, wpl, pos.
x(), height - pos.
y(), width, height,
756 ICOORD gradient = prev_gradient + next_gradient;
763 if (pt1.
y == pt2.
y && abs(gradient.y()) * 2 >= abs(gradient.x())) {
765 int diff_sign = (pt1.
x > pt2.
x) == negative ? 1 : -1;
766 int x =
MIN(pt1.
x, pt2.
x);
767 int y = height - pt1.
y;
770 EvaluateVerticalDiff(data, wpl, diff_sign, x, y, height,
771 &best_diff, &best_sum, &best_y);
776 }
while (EvaluateVerticalDiff(data, wpl, diff_sign, x, test_y, height,
777 &best_diff, &best_sum, &best_y));
781 }
while (EvaluateVerticalDiff(data, wpl, diff_sign, x, test_y, height,
782 &best_diff, &best_sum, &best_y));
783 offset = diff_sign * (best_sum / 2 - threshold) +
784 (y - best_y) * best_diff;
785 }
else if (pt1.
x == pt2.
x && abs(gradient.x()) * 2 >= abs(gradient.y())) {
787 int diff_sign = (pt1.
y > pt2.
y) == negative ? 1 : -1;
789 int y = height -
MAX(pt1.
y, pt2.
y);
790 const l_uint32* line = pixGetData(pix) + y * wpl;
793 EvaluateHorizontalDiff(line, diff_sign, x, width,
794 &best_diff, &best_sum, &best_x);
799 }
while (EvaluateHorizontalDiff(line, diff_sign, test_x, width,
800 &best_diff, &best_sum, &best_x));
804 }
while (EvaluateHorizontalDiff(line, diff_sign, test_x, width,
805 &best_diff, &best_sum, &best_x));
806 offset = diff_sign * (threshold - best_sum / 2) +
807 (best_x - x) * best_diff;
813 if (negative) gradient = -gradient;
818 prev_gradient = next_gradient;
859 memset(dir_counts, 0,
sizeof(dir_counts));
860 memset(pos_totals, 0,
sizeof(pos_totals));
865 tail_pos -=
step(stepcount - 1);
866 tail_pos -=
step(stepcount - 2);
869 ICOORD head_pos = tail_pos;
871 for (
int s = -2; s < 2; ++s) {
872 increment_step(s, 1, &head_pos, dir_counts, pos_totals);
874 for (
int s = 0; s < stepcount; pos +=
step(s++)) {
876 increment_step(s + 2, 1, &head_pos, dir_counts, pos_totals);
883 if (dir_counts[dir_index] >= 2 || (dir_counts[dir_index] == 1 &&
884 dir_counts[
Modulo(dir_index - 1, 4)] == 2 &&
885 dir_counts[
Modulo(dir_index + 1, 4)] == 2)) {
887 best_diff = dir_counts[dir_index];
888 int edge_pos = step_vec.
x() == 0 ? pos.
x() : pos.
y();
892 offset = pos_totals[dir_index] - best_diff * edge_pos;
901 increment_step(s - 2, -1, &tail_pos, dir_counts, pos_totals);
911 for (
int stepindex = 0; stepindex < stepcount; ++stepindex) {
913 if (next_step.
y() < 0) {
914 pixRasterop(pix, 0, top - pos.
y(), pos.
x() - left, 1,
915 PIX_NOT(PIX_DST),
NULL, 0, 0);
916 }
else if (next_step.
y() > 0) {
917 pixRasterop(pix, 0, top - pos.
y() - 1, pos.
x() - left, 1,
918 PIX_NOT(PIX_DST),
NULL, 0, 0);
933 for (
int stepindex = 0; stepindex < stepcount; ++stepindex) {
935 if (next_step.
y() < 0) {
936 pixSetPixel(pix, pos.
x() - left, top - pos.
y(), 1);
937 }
else if (next_step.
y() > 0) {
938 pixSetPixel(pix, pos.
x() - left - 1, top - pos.
y() - 1, 1);
939 }
else if (next_step.
x() < 0) {
940 pixSetPixel(pix, pos.
x() - left - 1, top - pos.
y(), 1);
941 }
else if (next_step.
x() > 0) {
942 pixSetPixel(pix, pos.
x() - left, top - pos.
y() - 1, 1);
956 #ifndef GRAPHICS_DISABLED
965 if (stepcount == 0) {
972 while (stepindex < stepcount) {
973 pos +=
step(stepindex);
977 while (stepindex < stepcount &&
979 pos +=
step(stepindex);
993 if (stepcount == 0) {
1004 for (
int s = 0; s < stepcount; pos +=
step(s++)) {
1006 if (edge_weight == 0) {
1029 start = source.start;
1032 stepcount = source.stepcount;
1034 memmove (steps, source.steps, step_mem());
1035 if (!children.empty ())
1037 children.deep_copy(&source.children, &
deep_copy);
1039 if (source.offsets !=
NULL) {
1041 memcpy(offsets, source.offsets, stepcount *
sizeof(*offsets));
1054 void C_OUTLINE::increment_step(
int s,
int increment,
ICOORD* pos,
1055 int* dir_counts,
int* pos_totals)
const {
1056 int step_index =
Modulo(s, stepcount);
1058 dir_counts[dir_index] += increment;
1060 if (step_vec.
x() == 0)
1061 pos_totals[dir_index] += pos->
x() * increment;
1063 pos_totals[dir_index] += pos->
y() * increment;
1068 return step_coords[chaindir % 4];
void set_x(inT16 xin)
rewrite function
void set_step(inT16 stepindex, inT8 stepdir)
void ComputeBinaryOffsets()
void free_mem(void *oldchunk)
#define ELISTIZE(CLASSNAME)
void DrawTo(int x, int y)
int direction(EDGEPT *point)
const ICOORD & start_pos() const
C_OUTLINE & operator=(const C_OUTLINE &source)
bool IsLegallyNested() const
void RemoveSmallRecursive(int min_size, C_OUTLINE_IT *it)
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
void rotate(const FCOORD &vec)
void move(const ICOORD vec)
inT16 y() const
access_function
void SetCursor(int x, int y)
int chain_code(int index) const
FCOORD sub_pixel_pos_at_index(const ICOORD &pos, int index) const
inT16 winding_number(ICOORD testpt) const
void render_outline(int left, int top, Pix *pix) const
inT32 count_transitions(inT32 threshold)
BOOL8 flag(C_OUTLINE_FLAGS mask) const
void NormTransform(const DENORM *first_norm, const TPOINT &pt, TPOINT *transformed) const
void set_y(inT16 yin)
rewrite function
DIR128 step_dir(int index) const
static ICOORD chain_step(int chaindir)
int edge_strength_at_index(int index) const
int IntCastRounded(double x)
inT16 x() const
access function
void plot_normed(const DENORM &denorm, ScrollView::Color colour, ScrollView *window) const
inT16 turn_direction() const
static void FakeOutline(const TBOX &box, C_OUTLINE_LIST *outlines)
void move(const ICOORD vec)
void Rectangle(int x1, int y1, int x2, int y2)
void ComputeEdgeOffsets(int threshold, Pix *pix)
bool contains(const FCOORD pt) const
ICOORD step(int index) const
void * alloc_mem(inT32 count)
static uinT8 binary_angle_plus_pi(double angle)
BOOL8 operator<(const C_OUTLINE &other) const
void render(int left, int top, Pix *pix) const
const DENORM * RootDenorm() const
bool overlap(const TBOX &box) const
void plot(ScrollView *window, ScrollView::Color colour) const
void rotate(const FCOORD &vec)
static C_OUTLINE * deep_copy(const C_OUTLINE *src)