home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / sel2path / edge.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-09  |  8.3 KB  |  267 lines

  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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include <assert.h>
  20.  
  21. #include "types.h"
  22. #include "sel2path.h"
  23. #include "edge.h"
  24.  
  25. /* We can move in any of eight directions as we are traversing
  26.    the outline.  These numbers are not arbitrary; TRY_PIXEL depends on
  27.    them.  */
  28.  
  29. typedef enum
  30. {
  31.   north = 0, northwest = 1, west = 2, southwest = 3, south = 4,
  32.   southeast = 5, east = 6, northeast = 7
  33. } direction_type;
  34.  
  35.  
  36. static boolean is_marked_edge (edge_type, unsigned, unsigned, bitmap_type);
  37. static boolean is_outline_edge (edge_type, unsigned, unsigned);
  38. static edge_type next_edge (edge_type);
  39.  
  40. /* The following macros are used (directly or indirectly) by the
  41.    `next_outline_edge' routine.  */ 
  42.  
  43. /* Given the direction DIR of the pixel to test, decide which edge on
  44.    that pixel we are supposed to test.  Because we've chosen the mapping
  45.    from directions to numbers carefully, we don't have to do much.  */
  46.  
  47. #define FIND_TEST_EDGE(dir) ((dir) / 2)
  48.  
  49.  
  50. /* Find how to move in direction DIR on the axis AXIS (either `ROW' or
  51.   `COL').   We are in the ``display'' coordinate system, with y
  52.   increasing downward and x increasing to the right.  Therefore, we are
  53.   implementing the following table:
  54.   
  55.   direction  row delta  col delta
  56.     north       -1          0  
  57.     south    +1        0
  58.     east     0       +1
  59.     west     0       +1
  60.     
  61.   with the other four directions (e.g., northwest) being the sum of
  62.   their components (e.g., north + west).
  63.   
  64.   The first macro, `COMPUTE_DELTA', handles splitting up the latter
  65.   cases, all of which have been assigned odd numbers.  */
  66.   
  67. #define COMPUTE_DELTA(axis, dir)                    \
  68.   ((dir) % 2 != 0                            \
  69.     ? COMPUTE_##axis##_DELTA ((dir) - 1)                \
  70.       + COMPUTE_##axis##_DELTA (((dir) + 1) % 8)            \
  71.     : COMPUTE_##axis##_DELTA (dir)                    \
  72.   )
  73.  
  74. /* Now it is trivial to implement the four cardinal directions.  */
  75. #define COMPUTE_ROW_DELTA(dir)                        \
  76.   ((dir) == north ? -1 : (dir) == south ? +1 : 0)
  77.  
  78. #define COMPUTE_COL_DELTA(dir)                        \
  79.   ((dir) == west ? -1 : (dir) == east ? +1 : 0)
  80.  
  81.  
  82. /* See if the appropriate edge on the pixel from (row,col) in direction
  83.    DIR is on the outline.  If so, update `row', `col', and `edge', and
  84.    break.  We also use the variable `character' as the bitmap in which
  85.    to look.  */
  86.  
  87. #define TRY_PIXEL(dir)                            \
  88.   {                                    \
  89.     int delta_r = COMPUTE_DELTA (ROW, dir);                \
  90.     int delta_c = COMPUTE_DELTA (COL, dir);                \
  91.     int test_row = *row + delta_r;                    \
  92.     int test_col = *col + delta_c;                    \
  93.     edge_type test_edge = FIND_TEST_EDGE (dir);                \
  94.                                     \
  95.     if (sel_valid_pixel(test_row, test_col)                       \
  96.         && is_outline_edge (test_edge, test_row, test_col))         \
  97.       {                                    \
  98.         *row = test_row;                        \
  99.         *col = test_col;                        \
  100.         *edge = test_edge;                        \
  101.         break;                                \
  102.       }                                    \
  103.   }
  104.  
  105. /* Finally, we are ready to implement the routine that finds the next
  106.    edge on the outline.  We look first for an adjacent edge that is not
  107.    on the current pixel.  We want to go around outside outlines
  108.    counterclockwise, and inside outlines clockwise (because that is how
  109.    both Metafont and Adobe Type 1 format want their curves to be drawn).
  110.    
  111.    The very first outline (an outside one) on each character starts on a
  112.    top edge (STARTING_EDGE in edge.h defines this); so, if we're at a
  113.    top edge, we want to go only to the left (on the pixel to the west)
  114.    or down (on the same pixel), to begin with.  Then, when we're on a
  115.    left edge, we want to go to the top edge (on the southwest pixel) or
  116.    to the left edge (on the south pixel).
  117.    
  118.    All well and good. But if you draw a rasterized circle (or whatever),
  119.    eventually we have to come back around to the beginning; at that
  120.    point, we'll be on a top edge, and we'll have to go to the right edge
  121.    on the northwest pixel.  Draw pictures.
  122.    
  123.    The upshot is, if we find an edge on another pixel, we return (in ROW
  124.    and COL) the position of the new pixel, and (in EDGE) the kind of
  125.    edge it is.  If we don't find such an edge, we return (in EDGE) the
  126.    next (in a counterclockwise direction) edge on the current pixel.  */
  127.  
  128. void
  129. next_outline_edge (edge_type *edge,
  130.            unsigned *row, unsigned *col)
  131. {
  132.   unsigned original_row = *row;
  133.   unsigned original_col = *col;
  134.   
  135.   switch (*edge)
  136.     {
  137.     case right:
  138.       TRY_PIXEL (north);
  139.       TRY_PIXEL (northeast);
  140.       break;
  141.  
  142.     case top:
  143.       TRY_PIXEL (west);
  144.       TRY_PIXEL (northwest);
  145.       break;
  146.  
  147.     case left:
  148.       TRY_PIXEL (south);
  149.       TRY_PIXEL (southwest);
  150.       break;
  151.  
  152.     case bottom:
  153.       TRY_PIXEL (east);
  154.       TRY_PIXEL (southeast);
  155.       break;
  156.  
  157.     default:
  158.       printf ("next_outline_edge: Bad edge value (%d)", *edge);
  159.  
  160.     }
  161.  
  162.   /* If we didn't find an adjacent edge on another pixel, return the
  163.      next edge on the current pixel.  */
  164.   if (*row == original_row && *col == original_col)
  165.     *edge = next_edge (*edge);
  166. }
  167.  
  168. /* We return the next edge on the pixel at position ROW and COL which is
  169.    an unmarked outline edge.  By ``next'' we mean either the one sent in
  170.    in STARTING_EDGE, if it qualifies, or the next such returned by
  171.    `next_edge'.  */
  172.  
  173. edge_type
  174. next_unmarked_outline_edge (unsigned row, unsigned col,
  175.                         edge_type starting_edge,
  176.                             bitmap_type marked)
  177. {
  178.   edge_type edge = starting_edge;
  179.  
  180.   assert (edge != no_edge);
  181.  
  182.   while (is_marked_edge (edge, row, col, marked)
  183.      || !is_outline_edge (edge, row, col))
  184.     {
  185.       edge = next_edge (edge);
  186.       if (edge == starting_edge)
  187.         return no_edge;
  188.     }
  189.  
  190.   return edge;
  191. }
  192.  
  193.  
  194. /* We check to see if the edge EDGE of the pixel at position ROW and COL
  195.    is an outline edge; i.e., that it is a black pixel which shares that
  196.    edge with a white pixel.  The position ROW and COL should be inside
  197.    the bitmap CHARACTER.  */
  198.  
  199. boolean
  200. is_outline_edge (edge_type edge,
  201.          unsigned row, unsigned col)
  202. {
  203.   /* If this pixel isn't black, it's not part of the outline.  */
  204.   if (sel_pixel_is_white(row, col))
  205.     return false;
  206.  
  207.   switch (edge)
  208.     {
  209.     case left:
  210.       return col == 0 || sel_pixel_is_white(row, col - 1);
  211.  
  212.     case top:
  213.       return row == 0 || sel_pixel_is_white(row - 1, col);
  214.  
  215.     case right:
  216.       return (col ==  sel_get_width() - 1) 
  217.     || sel_pixel_is_white(row, col + 1);
  218.  
  219.     case bottom:
  220.       return (row ==  sel_get_height() - 1) 
  221.     || sel_pixel_is_white(row + 1, col);
  222.  
  223.     case no_edge:
  224.     default:
  225.       printf ("is_outline_edge: Bad edge value(%d)", edge);
  226.     }
  227.   
  228.   return 0; /* NOTREACHED */
  229. }
  230.  
  231. /* If EDGE is not already marked, we mark it; otherwise, it's a fatal error.
  232.    The position ROW and COL should be inside the bitmap MARKED.  EDGE can
  233.    be `no_edge'; we just return false.  */
  234.  
  235. void
  236. mark_edge (edge_type edge, unsigned row, unsigned col, bitmap_type *marked)
  237. {
  238.   /* printf("row = %d, col = %d \n",row,col); */
  239.   assert (!is_marked_edge (edge, row, col, *marked));
  240.  
  241.   if (edge != no_edge)
  242.     BITMAP_PIXEL (*marked, row, col) |= 1 << edge;
  243. }
  244.  
  245.  
  246. /* Test if the edge EDGE at ROW/COL in MARKED is marked.  */
  247.  
  248. static boolean
  249. is_marked_edge (edge_type edge, unsigned row, unsigned col, bitmap_type marked)
  250. {
  251.   return
  252.     edge == no_edge ? false : BITMAP_PIXEL (marked, row, col) & (1 << edge);
  253. }
  254.  
  255.  
  256. /* Return the edge which is counterclockwise-adjacent to EDGE.  This
  257.    code makes use of the ``numericness'' of C enumeration constants;
  258.    sorry about that.  */
  259.  
  260. #define NUM_EDGES no_edge
  261.  
  262. static edge_type
  263. next_edge (edge_type edge)
  264. {
  265.   return edge == no_edge ? edge : (edge + 1) % NUM_EDGES;
  266. }
  267.