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 / curve.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-03-11  |  4.6 KB  |  184 lines

  1. /* curve.c: operations on the lists of pixels and lists of curves.
  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 <stdio.h>
  20. #include <glib.h>
  21.  
  22. #include "config.h"
  23. #include "global.h"
  24. #include "curve.h"
  25.  
  26.  
  27. /* Return an entirely empty curve.  */
  28.  
  29. curve_type
  30. new_curve ()
  31. {
  32.   curve_type curve = g_new (struct curve, 1);
  33.  
  34.   curve->point_list = NULL;
  35.   CURVE_LENGTH (curve) = 0;
  36.   CURVE_CYCLIC (curve) = false;
  37.   CURVE_START_TANGENT (curve) = CURVE_END_TANGENT (curve) = NULL;
  38.   PREVIOUS_CURVE (curve) = NEXT_CURVE (curve) = NULL;
  39.  
  40.   return curve;
  41. }
  42.  
  43.  
  44. /* Start the returned curve off with COORD as the first point.  */
  45.  
  46. curve_type
  47. init_curve (coordinate_type coord)
  48. {
  49.   curve_type curve = new_curve ();
  50.  
  51.   curve->point_list = g_new (point_type, 1);
  52.   CURVE_LENGTH (curve) = 1;
  53.  
  54.   CURVE_POINT (curve, 0) = int_to_real_coord (coord);
  55.  
  56.   return curve;
  57. }
  58.  
  59.  
  60. /* Don't copy the points or tangents, but copy everything else.  */
  61.  
  62. curve_type
  63. copy_most_of_curve (curve_type old_curve)
  64. {
  65.   curve_type curve = new_curve ();
  66.  
  67.   CURVE_CYCLIC (curve) = CURVE_CYCLIC (old_curve);
  68.   PREVIOUS_CURVE (curve) = PREVIOUS_CURVE (old_curve);
  69.   NEXT_CURVE (curve) = NEXT_CURVE (old_curve);
  70.  
  71.   return curve;
  72. }
  73.  
  74.  
  75. /* The length of CURVE will be zero if we ended up not being able to fit
  76.    it (which in turn implies a problem elsewhere in the program, but at
  77.    any rate, we shouldn't try here to free the nonexistent curve).  */
  78.  
  79. void
  80. free_curve (curve_type curve)
  81. {
  82.   if (CURVE_LENGTH (curve) > 0)
  83.     safe_free ((address *) &(curve->point_list));
  84. }
  85.  
  86.  
  87. void
  88. append_pixel (curve_type curve, coordinate_type coord)
  89. {
  90.   append_point (curve, int_to_real_coord (coord));
  91. }
  92.  
  93.  
  94. void
  95. append_point (curve_type curve, real_coordinate_type coord)
  96. {
  97.   CURVE_LENGTH (curve)++;
  98.   curve->point_list = g_realloc (curve->point_list,CURVE_LENGTH (curve) * sizeof(point_type));
  99.   LAST_CURVE_POINT (curve) = coord;
  100.   /* The t value does not need to be set.  */
  101. }
  102.  
  103. /* Return an initialized but empty curve list.  */
  104.  
  105. curve_list_type
  106. new_curve_list ()
  107. {
  108.   curve_list_type curve_list;
  109.  
  110.   curve_list.length = 0;
  111.   curve_list.data = NULL;
  112.  
  113.   return curve_list;
  114. }
  115.  
  116.  
  117. /* Free a curve list and all the curves it contains.  */
  118.  
  119. void
  120. free_curve_list (curve_list_type *curve_list)
  121. {
  122.   unsigned this_curve;
  123.  
  124.   for (this_curve = 0; this_curve < curve_list->length; this_curve++)
  125.     free_curve (curve_list->data[this_curve]);
  126.  
  127.   /* If the character was empty, it won't have any curves.  */
  128.   if (curve_list->data != NULL)
  129.     safe_free ((address *) &(curve_list->data));
  130. }
  131.  
  132.  
  133. /* Add an element to a curve list.  */
  134.  
  135. void
  136. append_curve (curve_list_type *curve_list, curve_type curve)
  137. {
  138.   curve_list->length++;
  139.   curve_list->data = g_realloc (curve_list->data,curve_list->length*sizeof(curve_type));
  140.   curve_list->data[curve_list->length - 1] = curve;
  141. }
  142.  
  143.  
  144. /* Return an initialized but empty curve list array.  */
  145.  
  146. curve_list_array_type
  147. new_curve_list_array ()
  148. {
  149.   curve_list_array_type curve_list_array;
  150.  
  151.   CURVE_LIST_ARRAY_LENGTH (curve_list_array) = 0;
  152.   curve_list_array.data = NULL;
  153.  
  154.   return curve_list_array;
  155. }
  156.  
  157.  
  158. /* Free a curve list array and all the curve lists it contains.  */
  159.  
  160. void
  161. free_curve_list_array (curve_list_array_type *curve_list_array)
  162. {
  163.   unsigned this_list;
  164.  
  165.   for (this_list = 0; this_list < CURVE_LIST_ARRAY_LENGTH (*curve_list_array);
  166.        this_list++) 
  167.     free_curve_list (&CURVE_LIST_ARRAY_ELT (*curve_list_array, this_list));
  168.  
  169.   /* If the character was empty, it won't have any curves.  */
  170.   if (curve_list_array->data != NULL)
  171.     safe_free ((address *) &(curve_list_array->data));
  172. }
  173.  
  174.  
  175. /* Add an element to a curve list array.  */
  176.  
  177. void
  178. append_curve_list (curve_list_array_type *l, curve_list_type curve_list)
  179. {
  180.   CURVE_LIST_ARRAY_LENGTH (*l)++;
  181.   l->data = g_realloc (l->data,( CURVE_LIST_ARRAY_LENGTH (*l))*sizeof(curve_list_type));
  182.   LAST_CURVE_LIST_ARRAY_ELT (*l) = curve_list;
  183. }
  184.