1
/* edge.c: operations on edges in bitmaps.
3
* Copyright (C) 1992 Free Software Foundation, Inc.
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2, or (at your option)
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28
/* We can move in any of eight directions as we are traversing
29
the outline. These numbers are not arbitrary; TRY_PIXEL depends on
34
north = 0, northwest = 1, west = 2, southwest = 3, south = 4,
35
southeast = 5, east = 6, northeast = 7
39
static boolean is_marked_edge (edge_type, unsigned, unsigned, bitmap_type);
40
static boolean is_outline_edge (edge_type, unsigned, unsigned);
41
static edge_type next_edge (edge_type);
43
/* The following macros are used (directly or indirectly) by the
44
`next_outline_edge' routine. */
46
/* Given the direction DIR of the pixel to test, decide which edge on
47
that pixel we are supposed to test. Because we've chosen the mapping
48
from directions to numbers carefully, we don't have to do much. */
50
#define FIND_TEST_EDGE(dir) ((dir) / 2)
53
/* Find how to move in direction DIR on the axis AXIS (either `ROW' or
54
`COL'). We are in the ``display'' coordinate system, with y
55
increasing downward and x increasing to the right. Therefore, we are
56
implementing the following table:
58
direction row delta col delta
64
with the other four directions (e.g., northwest) being the sum of
65
their components (e.g., north + west).
67
The first macro, `COMPUTE_DELTA', handles splitting up the latter
68
cases, all of which have been assigned odd numbers. */
70
#define COMPUTE_DELTA(axis, dir) \
72
? COMPUTE_##axis##_DELTA ((dir) - 1) \
73
+ COMPUTE_##axis##_DELTA (((dir) + 1) % 8) \
74
: COMPUTE_##axis##_DELTA (dir) \
77
/* Now it is trivial to implement the four cardinal directions. */
78
#define COMPUTE_ROW_DELTA(dir) \
79
((dir) == north ? -1 : (dir) == south ? +1 : 0)
81
#define COMPUTE_COL_DELTA(dir) \
82
((dir) == west ? -1 : (dir) == east ? +1 : 0)
85
/* See if the appropriate edge on the pixel from (row,col) in direction
86
DIR is on the outline. If so, update `row', `col', and `edge', and
87
break. We also use the variable `character' as the bitmap in which
90
#define TRY_PIXEL(dir) \
92
int delta_r = COMPUTE_DELTA (ROW, dir); \
93
int delta_c = COMPUTE_DELTA (COL, dir); \
94
int test_row = *row + delta_r; \
95
int test_col = *col + delta_c; \
96
edge_type test_edge = FIND_TEST_EDGE (dir); \
98
if (sel_valid_pixel(test_row, test_col) \
99
&& is_outline_edge (test_edge, test_row, test_col)) \
108
/* Finally, we are ready to implement the routine that finds the next
109
edge on the outline. We look first for an adjacent edge that is not
110
on the current pixel. We want to go around outside outlines
111
counterclockwise, and inside outlines clockwise (because that is how
112
both Metafont and Adobe Type 1 format want their curves to be drawn).
114
The very first outline (an outside one) on each character starts on a
115
top edge (STARTING_EDGE in edge.h defines this); so, if we're at a
116
top edge, we want to go only to the left (on the pixel to the west)
117
or down (on the same pixel), to begin with. Then, when we're on a
118
left edge, we want to go to the top edge (on the southwest pixel) or
119
to the left edge (on the south pixel).
121
All well and good. But if you draw a rasterized circle (or whatever),
122
eventually we have to come back around to the beginning; at that
123
point, we'll be on a top edge, and we'll have to go to the right edge
124
on the northwest pixel. Draw pictures.
126
The upshot is, if we find an edge on another pixel, we return (in ROW
127
and COL) the position of the new pixel, and (in EDGE) the kind of
128
edge it is. If we don't find such an edge, we return (in EDGE) the
129
next (in a counterclockwise direction) edge on the current pixel. */
132
next_outline_edge (edge_type *edge,
133
unsigned *row, unsigned *col)
135
unsigned original_row = *row;
136
unsigned original_col = *col;
142
TRY_PIXEL (northeast);
147
TRY_PIXEL (northwest);
152
TRY_PIXEL (southwest);
157
TRY_PIXEL (southeast);
161
printf ("next_outline_edge: Bad edge value (%d)", *edge);
165
/* If we didn't find an adjacent edge on another pixel, return the
166
next edge on the current pixel. */
167
if (*row == original_row && *col == original_col)
168
*edge = next_edge (*edge);
171
/* We return the next edge on the pixel at position ROW and COL which is
172
an unmarked outline edge. By ``next'' we mean either the one sent in
173
in STARTING_EDGE, if it qualifies, or the next such returned by
177
next_unmarked_outline_edge (unsigned row, unsigned col,
178
edge_type starting_edge,
181
edge_type edge = starting_edge;
183
assert (edge != no_edge);
185
while (is_marked_edge (edge, row, col, marked)
186
|| !is_outline_edge (edge, row, col))
188
edge = next_edge (edge);
189
if (edge == starting_edge)
197
/* We check to see if the edge EDGE of the pixel at position ROW and COL
198
is an outline edge; i.e., that it is a black pixel which shares that
199
edge with a white pixel. The position ROW and COL should be inside
200
the bitmap CHARACTER. */
203
is_outline_edge (edge_type edge,
204
unsigned row, unsigned col)
206
/* If this pixel isn't black, it's not part of the outline. */
207
if (sel_pixel_is_white(row, col))
213
return col == 0 || sel_pixel_is_white(row, col - 1);
216
return row == 0 || sel_pixel_is_white(row - 1, col);
219
return (col == sel_get_width() - 1)
220
|| sel_pixel_is_white(row, col + 1);
223
return (row == sel_get_height() - 1)
224
|| sel_pixel_is_white(row + 1, col);
228
printf ("is_outline_edge: Bad edge value(%d)", edge);
231
return 0; /* NOTREACHED */
234
/* If EDGE is not already marked, we mark it; otherwise, it's a fatal error.
235
The position ROW and COL should be inside the bitmap MARKED. EDGE can
236
be `no_edge'; we just return false. */
239
mark_edge (edge_type edge, unsigned row, unsigned col, bitmap_type *marked)
241
/* printf("row = %d, col = %d \n",row,col); */
242
assert (!is_marked_edge (edge, row, col, *marked));
245
BITMAP_PIXEL (*marked, row, col) |= 1 << edge;
249
/* Test if the edge EDGE at ROW/COL in MARKED is marked. */
252
is_marked_edge (edge_type edge, unsigned row, unsigned col, bitmap_type marked)
255
edge == no_edge ? false : BITMAP_PIXEL (marked, row, col) & (1 << edge);
259
/* Return the edge which is counterclockwise-adjacent to EDGE. This
260
code makes use of the ``numericness'' of C enumeration constants;
263
#define NUM_EDGES no_edge
266
next_edge (edge_type edge)
268
return edge == no_edge ? edge : (edge + 1) % NUM_EDGES;