27 #include "allheaders.h" 52 ):box (bot_left, top_right), start (startpt->pos), offsets(
NULL) {
63 memset(steps, 0, step_mem());
66 for (stepindex = 0; stepindex < length; stepindex++) {
69 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;
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)
345 BOOL8 first_was_max_x;
346 BOOL8 first_was_max_y;
347 BOOL8 looking_for_max_x;
348 BOOL8 looking_for_min_x;
349 BOOL8 looking_for_max_y;
350 BOOL8 looking_for_min_y;
354 inT32 max_x, min_x, max_y, min_y;
355 inT32 initial_x, initial_y;
363 max_x = min_x = pos.
x ();
364 max_y = min_y = pos.
y ();
365 looking_for_max_x =
TRUE;
366 looking_for_min_x =
TRUE;
367 looking_for_max_y =
TRUE;
368 looking_for_min_y =
TRUE;
369 first_was_max_x =
FALSE;
370 first_was_max_y =
FALSE;
371 initial_x = pos.
x ();
372 initial_y = pos.
y ();
373 for (stepindex = 0; stepindex < total_steps; stepindex++) {
375 next_step =
step (stepindex);
377 if (next_step.
x () < 0) {
378 if (looking_for_max_x && pos.
x () < min_x)
380 if (looking_for_min_x && max_x - pos.
x () > threshold) {
381 if (looking_for_max_x) {
383 first_was_max_x =
FALSE;
386 looking_for_max_x =
TRUE;
387 looking_for_min_x =
FALSE;
391 else if (next_step.
x () > 0) {
392 if (looking_for_min_x && pos.
x () > max_x)
394 if (looking_for_max_x && pos.
x () - min_x > threshold) {
395 if (looking_for_min_x) {
397 first_was_max_x =
TRUE;
400 looking_for_max_x =
FALSE;
401 looking_for_min_x =
TRUE;
405 else if (next_step.
y () < 0) {
406 if (looking_for_max_y && pos.
y () < min_y)
408 if (looking_for_min_y && max_y - pos.
y () > threshold) {
409 if (looking_for_max_y) {
411 first_was_max_y =
FALSE;
414 looking_for_max_y =
TRUE;
415 looking_for_min_y =
FALSE;
420 if (looking_for_min_y && pos.
y () > max_y)
422 if (looking_for_max_y && pos.
y () - min_y > threshold) {
423 if (looking_for_min_y) {
425 first_was_max_y =
TRUE;
428 looking_for_max_y =
FALSE;
429 looking_for_min_y =
TRUE;
435 if (first_was_max_x && looking_for_min_x) {
436 if (max_x - initial_x > threshold)
441 else if (!first_was_max_x && looking_for_max_x) {
442 if (initial_x - min_x > threshold)
447 if (first_was_max_y && looking_for_min_y) {
448 if (max_y - initial_y > threshold)
453 else if (!first_was_max_y && looking_for_max_y) {
454 if (initial_y - min_y > threshold)
482 return other.box.
contains(this->box);
485 for (stepindex = 0; stepindex < stepcount
487 pos +=
step (stepindex);
491 for (stepindex = 0; stepindex < other.stepcount
494 pos += other.
step (stepindex);
518 for (stepindex = 0; stepindex < stepcount; stepindex++) {
519 stepvec =
step (stepindex);
521 if (vec.
y () <= 0 && vec.
y () + stepvec.
y () > 0) {
522 cross = vec * stepvec;
528 else if (vec.
y () > 0 && vec.
y () + stepvec.
y () <= 0) {
529 cross = vec * stepvec;
558 for (stepindex = 0; stepindex < stepcount; stepindex++) {
560 dirdiff = dir - prevdir;
561 ASSERT_HOST (dirdiff == 0 || dirdiff == 32 || dirdiff == -32);
583 halfsteps = (stepcount + 1) / 2;
584 for (stepindex = 0; stepindex < halfsteps; stepindex++) {
585 farindex = stepcount - stepindex - 1;
588 set_step (farindex, stepdir + halfturn);
602 C_OUTLINE_IT it(&children);
607 for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ())
608 it.data ()->move (vec);
616 if (stepcount == 0)
return true;
620 C_OUTLINE_IT child_it(const_cast<C_OUTLINE_LIST*>(&children));
621 for (child_it.mark_cycle_pt(); !child_it.cycled_list(); child_it.forward()) {
635 if (box.
width() < min_size || box.
height() < min_size) {
637 delete it->extract();
638 }
else if (!children.empty()) {
640 C_OUTLINE_IT child_it(&children);
641 for (child_it.mark_cycle_pt(); !child_it.cycled_list();
642 child_it.forward()) {
656 static void ComputeGradient(
const l_uint32* data,
int wpl,
657 int x,
int y,
int width,
int height,
659 const l_uint32* line = data + y * wpl;
660 int pix_x_y = x < width && y < height ?
661 GET_DATA_BYTE(const_cast<void*> (reinterpret_cast<const void *>(line)), x) : 255;
662 int pix_x_prevy = x < width && y > 0 ?
663 GET_DATA_BYTE(const_cast<void*> (reinterpret_cast<const void *>(line - wpl)), x) : 255;
664 int pix_prevx_prevy = x > 0 && y > 0 ?
665 GET_DATA_BYTE(const_cast<void*> (reinterpret_cast<void const*>(line - wpl)), x - 1) : 255;
666 int pix_prevx_y = x > 0 && y < height ?
667 GET_DATA_BYTE(const_cast<void*> (reinterpret_cast<const void *>(line)), x - 1) : 255;
668 gradient->
set_x(pix_x_y + pix_x_prevy - (pix_prevx_y + pix_prevx_prevy));
669 gradient->
set_y(pix_x_prevy + pix_prevx_prevy - (pix_x_y + pix_prevx_y));
675 static bool EvaluateVerticalDiff(
const l_uint32* data,
int wpl,
int diff_sign,
676 int x,
int y,
int height,
677 int* best_diff,
int* best_sum,
int* best_y) {
678 if (y <= 0 || y >= height)
680 const l_uint32* line = data + y * wpl;
681 int pixel1 = GET_DATA_BYTE(const_cast<void*> (reinterpret_cast<const void *>(line - wpl)), x);
682 int pixel2 = GET_DATA_BYTE(const_cast<void*> (reinterpret_cast<const void *>(line)), x);
683 int diff = (pixel2 - pixel1) * diff_sign;
684 if (diff > *best_diff) {
686 *best_sum = pixel1 + pixel2;
695 static bool EvaluateHorizontalDiff(
const l_uint32* line,
int diff_sign,
697 int* best_diff,
int* best_sum,
int* best_x) {
698 if (x <= 0 || x >= width)
700 int pixel1 = GET_DATA_BYTE(const_cast<void*> (reinterpret_cast<const void *>(line)), x - 1);
701 int pixel2 = GET_DATA_BYTE(const_cast<void*> (reinterpret_cast<const void *>(line)), x);
702 int diff = (pixel2 - pixel1) * diff_sign;
703 if (diff > *best_diff) {
705 *best_sum = pixel1 + pixel2;
723 if (pixGetDepth(pix) != 8)
return;
724 const l_uint32* data = pixGetData(pix);
725 int wpl = pixGetWpl(pix);
726 int width = pixGetWidth(pix);
727 int height = pixGetHeight(pix);
733 ComputeGradient(data, wpl, pos.
x(), height - pos.
y(), width, height,
735 for (
int s = 0; s < stepcount; ++s) {
741 ComputeGradient(data, wpl, pos.
x(), height - pos.
y(), width, height,
744 ICOORD gradient = prev_gradient + next_gradient;
751 if (pt1.
y == pt2.
y && abs(gradient.y()) * 2 >= abs(gradient.x())) {
753 int diff_sign = (pt1.
x > pt2.
x) == negative ? 1 : -1;
754 int x =
MIN(pt1.
x, pt2.
x);
755 int y = height - pt1.
y;
758 EvaluateVerticalDiff(data, wpl, diff_sign, x, y, height,
759 &best_diff, &best_sum, &best_y);
764 }
while (EvaluateVerticalDiff(data, wpl, diff_sign, x, test_y, height,
765 &best_diff, &best_sum, &best_y));
769 }
while (EvaluateVerticalDiff(data, wpl, diff_sign, x, test_y, height,
770 &best_diff, &best_sum, &best_y));
771 offset = diff_sign * (best_sum / 2 - threshold) +
772 (y - best_y) * best_diff;
773 }
else if (pt1.
x == pt2.
x && abs(gradient.x()) * 2 >= abs(gradient.y())) {
775 int diff_sign = (pt1.
y > pt2.
y) == negative ? 1 : -1;
777 int y = height -
MAX(pt1.
y, pt2.
y);
778 const l_uint32* line = pixGetData(pix) + y * wpl;
781 EvaluateHorizontalDiff(line, diff_sign, x, width,
782 &best_diff, &best_sum, &best_x);
787 }
while (EvaluateHorizontalDiff(line, diff_sign, test_x, width,
788 &best_diff, &best_sum, &best_x));
792 }
while (EvaluateHorizontalDiff(line, diff_sign, test_x, width,
793 &best_diff, &best_sum, &best_x));
794 offset = diff_sign * (threshold - best_sum / 2) +
795 (best_x - x) * best_diff;
801 if (negative) gradient = -gradient;
806 prev_gradient = next_gradient;
842 memset(dir_counts, 0,
sizeof(dir_counts));
843 memset(pos_totals, 0,
sizeof(pos_totals));
848 tail_pos -=
step(stepcount - 1);
849 tail_pos -=
step(stepcount - 2);
852 ICOORD head_pos = tail_pos;
854 for (
int s = -2; s < 2; ++s) {
855 increment_step(s, 1, &head_pos, dir_counts, pos_totals);
857 for (
int s = 0; s < stepcount; pos +=
step(s++)) {
859 increment_step(s + 2, 1, &head_pos, dir_counts, pos_totals);
866 if (dir_counts[dir_index] >= 2 || (dir_counts[dir_index] == 1 &&
867 dir_counts[
Modulo(dir_index - 1, 4)] == 2 &&
868 dir_counts[
Modulo(dir_index + 1, 4)] == 2)) {
870 best_diff = dir_counts[dir_index];
871 int edge_pos = step_vec.
x() == 0 ? pos.
x() : pos.
y();
875 offset = pos_totals[dir_index] - best_diff * edge_pos;
884 increment_step(s - 2, -1, &tail_pos, dir_counts, pos_totals);
892 for (
int stepindex = 0; stepindex < stepcount; ++stepindex) {
894 if (next_step.
y() < 0) {
895 pixRasterop(pix, 0, top - pos.
y(), pos.
x() - left, 1,
896 PIX_NOT(PIX_DST),
NULL, 0, 0);
897 }
else if (next_step.
y() > 0) {
898 pixRasterop(pix, 0, top - pos.
y() - 1, pos.
x() - left, 1,
899 PIX_NOT(PIX_DST),
NULL, 0, 0);
909 for (
int stepindex = 0; stepindex < stepcount; ++stepindex) {
911 if (next_step.
y() < 0) {
912 pixSetPixel(pix, pos.
x() - left, top - pos.
y(), 1);
913 }
else if (next_step.
y() > 0) {
914 pixSetPixel(pix, pos.
x() - left - 1, top - pos.
y() - 1, 1);
915 }
else if (next_step.
x() < 0) {
916 pixSetPixel(pix, pos.
x() - left - 1, top - pos.
y(), 1);
917 }
else if (next_step.
x() > 0) {
918 pixSetPixel(pix, pos.
x() - left, top - pos.
y() - 1, 1);
930 #ifndef GRAPHICS_DISABLED 941 if (stepcount == 0) {
948 while (stepindex < stepcount) {
949 pos +=
step(stepindex);
953 while (stepindex < stepcount &&
955 pos +=
step(stepindex);
966 if (stepcount == 0) {
977 for (
int s = 0; s < stepcount; pos +=
step(s++)) {
979 if (edge_weight == 0) {
1004 start = source.start;
1007 stepcount = source.stepcount;
1009 memmove (steps, source.steps, step_mem());
1010 if (!children.empty ())
1012 children.deep_copy(&source.children, &
deep_copy);
1014 if (source.offsets !=
NULL) {
1016 memcpy(offsets, source.offsets, stepcount *
sizeof(*offsets));
1027 void C_OUTLINE::increment_step(
int s,
int increment,
ICOORD* pos,
1028 int* dir_counts,
int* pos_totals)
const {
1029 int step_index =
Modulo(s, stepcount);
1031 dir_counts[dir_index] += increment;
1033 if (step_vec.
x() == 0)
1034 pos_totals[dir_index] += pos->
x() * increment;
1036 pos_totals[dir_index] += pos->
y() * increment;
1041 return step_coords[chaindir % 4];
inT16 turn_direction() const
void render_outline(int left, int top, Pix *pix) const
int IntCastRounded(double x)
void SetCursor(int x, int y)
inT32 count_transitions(inT32 threshold)
void rotate(const FCOORD &vec)
bool overlap(const TBOX &box) const
void plot(ScrollView *window, ScrollView::Color colour) const
static void FakeOutline(const TBOX &box, C_OUTLINE_LIST *outlines)
static C_OUTLINE * deep_copy(const C_OUTLINE *src)
void Rectangle(int x1, int y1, int x2, int y2)
BOOL8 flag(C_OUTLINE_FLAGS mask) const
void ComputeBinaryOffsets()
DIR128 step_dir(int index) const
static ICOORD chain_step(int chaindir)
void set_x(inT16 xin)
rewrite function
ICOORD step(int index) const
#define ELISTIZE(CLASSNAME)
C_OUTLINE & operator=(const C_OUTLINE &source)
static uinT8 binary_angle_plus_pi(double angle)
BOOL8 operator<(const C_OUTLINE &other) const
void render(int left, int top, Pix *pix) const
void * alloc_mem(inT32 count)
void NormTransform(const DENORM *first_norm, const TPOINT &pt, TPOINT *transformed) const
void set_y(inT16 yin)
rewrite function
int direction(EDGEPT *point)
inT16 x() const
access function
void plot_normed(const DENORM &denorm, ScrollView::Color colour, ScrollView *window) const
void DrawTo(int x, int y)
void ComputeEdgeOffsets(int threshold, Pix *pix)
void set_step(inT16 stepindex, inT8 stepdir)
const ICOORD & start_pos() const
void move(const ICOORD vec)
const DENORM * RootDenorm() const
void RemoveSmallRecursive(int min_size, C_OUTLINE_IT *it)
bool contains(const FCOORD pt) const
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
void move(const ICOORD vec)
void free_mem(void *oldchunk)
inT16 y() const
access_function
bool IsLegallyNested() const
void rotate(const FCOORD &vec)
int chain_code(int index) const
FCOORD sub_pixel_pos_at_index(const ICOORD &pos, int index) const
inT16 winding_number(ICOORD testpt) const
int edge_strength_at_index(int index) const