~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to plug-ins/sel2path/edge.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2008-10-06 13:30:41 UTC
  • mto: This revision was merged to the branch mainline in revision 35.
  • Revision ID: james.westby@ubuntu.com-20081006133041-3panbkcanaymfsmp
Tags: upstream-2.6.0
ImportĀ upstreamĀ versionĀ 2.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* edge.c: operations on edges in bitmaps.
2
 
 *
3
 
 * Copyright (C) 1992 Free Software Foundation, Inc.
4
 
 *
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)
8
 
 * any later version.
9
 
 *
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.
14
 
 *
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.
18
 
 */
19
 
 
20
 
#include "config.h"
21
 
 
22
 
#include <assert.h>
23
 
 
24
 
#include "types.h"
25
 
#include "sel2path.h"
26
 
#include "edge.h"
27
 
 
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
30
 
   them.  */
31
 
 
32
 
typedef enum
33
 
{
34
 
  north = 0, northwest = 1, west = 2, southwest = 3, south = 4,
35
 
  southeast = 5, east = 6, northeast = 7
36
 
} direction_type;
37
 
 
38
 
 
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);
42
 
 
43
 
/* The following macros are used (directly or indirectly) by the
44
 
   `next_outline_edge' routine.  */
45
 
 
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.  */
49
 
 
50
 
#define FIND_TEST_EDGE(dir) ((dir) / 2)
51
 
 
52
 
 
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:
57
 
 
58
 
  direction  row delta  col delta
59
 
    north       -1          0
60
 
    south       +1          0
61
 
    east         0         +1
62
 
    west         0         +1
63
 
 
64
 
  with the other four directions (e.g., northwest) being the sum of
65
 
  their components (e.g., north + west).
66
 
 
67
 
  The first macro, `COMPUTE_DELTA', handles splitting up the latter
68
 
  cases, all of which have been assigned odd numbers.  */
69
 
 
70
 
#define COMPUTE_DELTA(axis, dir)                                        \
71
 
  ((dir) % 2 != 0                                                       \
72
 
    ? COMPUTE_##axis##_DELTA ((dir) - 1)                                \
73
 
      + COMPUTE_##axis##_DELTA (((dir) + 1) % 8)                        \
74
 
    : COMPUTE_##axis##_DELTA (dir)                                      \
75
 
  )
76
 
 
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)
80
 
 
81
 
#define COMPUTE_COL_DELTA(dir)                                          \
82
 
  ((dir) == west ? -1 : (dir) == east ? +1 : 0)
83
 
 
84
 
 
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
88
 
   to look.  */
89
 
 
90
 
#define TRY_PIXEL(dir)                                                  \
91
 
  {                                                                     \
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);                         \
97
 
                                                                        \
98
 
    if (sel_valid_pixel(test_row, test_col)                             \
99
 
        && is_outline_edge (test_edge, test_row, test_col))             \
100
 
      {                                                                 \
101
 
        *row = test_row;                                                \
102
 
        *col = test_col;                                                \
103
 
        *edge = test_edge;                                              \
104
 
        break;                                                          \
105
 
      }                                                                 \
106
 
  }
107
 
 
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).
113
 
 
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).
120
 
 
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.
125
 
 
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.  */
130
 
 
131
 
void
132
 
next_outline_edge (edge_type *edge,
133
 
                   unsigned *row, unsigned *col)
134
 
{
135
 
  unsigned original_row = *row;
136
 
  unsigned original_col = *col;
137
 
 
138
 
  switch (*edge)
139
 
    {
140
 
    case right:
141
 
      TRY_PIXEL (north);
142
 
      TRY_PIXEL (northeast);
143
 
      break;
144
 
 
145
 
    case top:
146
 
      TRY_PIXEL (west);
147
 
      TRY_PIXEL (northwest);
148
 
      break;
149
 
 
150
 
    case left:
151
 
      TRY_PIXEL (south);
152
 
      TRY_PIXEL (southwest);
153
 
      break;
154
 
 
155
 
    case bottom:
156
 
      TRY_PIXEL (east);
157
 
      TRY_PIXEL (southeast);
158
 
      break;
159
 
 
160
 
    default:
161
 
      printf ("next_outline_edge: Bad edge value (%d)", *edge);
162
 
 
163
 
    }
164
 
 
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);
169
 
}
170
 
 
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
174
 
   `next_edge'.  */
175
 
 
176
 
edge_type
177
 
next_unmarked_outline_edge (unsigned row, unsigned col,
178
 
                            edge_type starting_edge,
179
 
                            bitmap_type marked)
180
 
{
181
 
  edge_type edge = starting_edge;
182
 
 
183
 
  assert (edge != no_edge);
184
 
 
185
 
  while (is_marked_edge (edge, row, col, marked)
186
 
         || !is_outline_edge (edge, row, col))
187
 
    {
188
 
      edge = next_edge (edge);
189
 
      if (edge == starting_edge)
190
 
        return no_edge;
191
 
    }
192
 
 
193
 
  return edge;
194
 
}
195
 
 
196
 
 
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.  */
201
 
 
202
 
boolean
203
 
is_outline_edge (edge_type edge,
204
 
                 unsigned row, unsigned col)
205
 
{
206
 
  /* If this pixel isn't black, it's not part of the outline.  */
207
 
  if (sel_pixel_is_white(row, col))
208
 
    return false;
209
 
 
210
 
  switch (edge)
211
 
    {
212
 
    case left:
213
 
      return col == 0 || sel_pixel_is_white(row, col - 1);
214
 
 
215
 
    case top:
216
 
      return row == 0 || sel_pixel_is_white(row - 1, col);
217
 
 
218
 
    case right:
219
 
      return (col ==  sel_get_width() - 1)
220
 
        || sel_pixel_is_white(row, col + 1);
221
 
 
222
 
    case bottom:
223
 
      return (row ==  sel_get_height() - 1)
224
 
        || sel_pixel_is_white(row + 1, col);
225
 
 
226
 
    case no_edge:
227
 
    default:
228
 
      printf ("is_outline_edge: Bad edge value(%d)", edge);
229
 
    }
230
 
 
231
 
  return 0; /* NOTREACHED */
232
 
}
233
 
 
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.  */
237
 
 
238
 
void
239
 
mark_edge (edge_type edge, unsigned row, unsigned col, bitmap_type *marked)
240
 
{
241
 
  /* printf("row = %d, col = %d \n",row,col); */
242
 
  assert (!is_marked_edge (edge, row, col, *marked));
243
 
 
244
 
  if (edge != no_edge)
245
 
    BITMAP_PIXEL (*marked, row, col) |= 1 << edge;
246
 
}
247
 
 
248
 
 
249
 
/* Test if the edge EDGE at ROW/COL in MARKED is marked.  */
250
 
 
251
 
static boolean
252
 
is_marked_edge (edge_type edge, unsigned row, unsigned col, bitmap_type marked)
253
 
{
254
 
  return
255
 
    edge == no_edge ? false : BITMAP_PIXEL (marked, row, col) & (1 << edge);
256
 
}
257
 
 
258
 
 
259
 
/* Return the edge which is counterclockwise-adjacent to EDGE.  This
260
 
   code makes use of the ``numericness'' of C enumeration constants;
261
 
   sorry about that.  */
262
 
 
263
 
#define NUM_EDGES no_edge
264
 
 
265
 
static edge_type
266
 
next_edge (edge_type edge)
267
 
{
268
 
  return edge == no_edge ? edge : (edge + 1) % NUM_EDGES;
269
 
}