-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfill.c
More file actions
173 lines (145 loc) · 3.54 KB
/
Copy pathfill.c
File metadata and controls
173 lines (145 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
fill.c
This is part of pcb2g - pcb bitmap to G code converter
Copyright (C) 2011- 2015 Peter Popovec, popovec@fei.tuke.sk
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
image fill functions
*/
#include <stdlib.h>
#include "pcb2g.h"
static struct fill_data v;
struct stack_point
{
int x, y;
// int level;
struct stack_point *next;
};
static struct stack_point *fill_stack;
static int fill_stack_level;
static int fill_stack_max_level;
static void
fill_push (int x, int y)
{
struct stack_point *s;
s = malloc (sizeof (struct stack_point));
s->x = x;
s->y = y;
s->next = fill_stack;
fill_stack = s;
fill_stack_level++;
if (fill_stack_level > fill_stack_max_level)
fill_stack_max_level = fill_stack_level;
}
static struct stack_point *
fill_pop ()
{
struct stack_point *s;
s = fill_stack;
if (s != NULL)
{
fill_stack = s->next;
fill_stack_level--;
}
return s;
}
struct fill_data *
image_fill (struct image *image, int vx, int vy, unsigned char fill_data,
unsigned char border)
{
struct stack_point *s;
int sx, xmin, xmax;
int f;
if (*(image->data + vx + vy * image->x) == border) //vyplneny
return NULL;
fill_stack_level = fill_stack_max_level = 0;
fill_push (vx, vy);
v.xmax = v.xmin = vx;
v.ymax = v.ymin = vy;
v.count = 1;
for (;;)
{
s = fill_pop ();
if (!s)
{
v.level = fill_stack_max_level;
return &v;
}
*(image->data + s->x + s->y * image->x) = fill_data; //vypln
xmin = xmax = s->x;
if (v.ymin > s->y)
v.ymin = s->y;
if (v.ymax < s->y)
v.ymax = s->y;
// printf ("scanline %d x at %d\n", s->y, s->x);
if (s->x > 0)
for (sx = s->x - 1; sx >= 0; sx--)
{
if (*(image->data + sx + s->y * image->x) == border) //treba vyplnit?
break;
*(image->data + sx + s->y * image->x) = fill_data;
v.count++;
xmin = sx;
}
// printf ("minX=%d\n", xmin);
if (s->x < image->x - 1)
for (sx = s->x + 1; sx < image->x; sx++)
{
if (*(image->data + sx + s->y * image->x) == border) //treba vyplnit?
break;
*(image->data + sx + s->y * image->x) = fill_data;
v.count++;
xmax = sx;
}
// printf ("maxX=%d\n", xmax);
if (v.xmin > xmin)
v.xmin = xmin;
if (v.xmax < xmax)
v.xmax = xmax;
if (s->y > 0)
{
f = 1;
for (sx = xmin; sx <= xmax; sx++)
{
if (*(image->data + sx + (s->y - 1) * image->x) != border
&& *(image->data + sx + (s->y - 1) * image->x) != fill_data)
{
if (f)
{
fill_push (sx, s->y - 1);
f = 0;
}
}
else
f = 1;
}
}
if (s->y < image->y - 1)
{
f = 1;
for (sx = xmin; sx <= xmax; sx++)
{
if (*(image->data + sx + (s->y + 1) * image->x) != border
&& *(image->data + sx + (s->y + 1) * image->x) != fill_data)
{
if (f)
{
fill_push (sx, s->y + 1);
f = 0;
}
}
else
f = 1;
}
}
free (s);
}
}