diff --git a/app/vectors/Makefile.am b/app/vectors/Makefile.am index eb2274b5aa..51549ed044 100644 --- a/app/vectors/Makefile.am +++ b/app/vectors/Makefile.am @@ -20,6 +20,8 @@ libappvectors_a_SOURCES = \ vectors-types.h \ gimpanchor.c \ gimpanchor.h \ + gimpbezierdesc.h \ + gimpbezierdesc.c \ gimpbezierstroke.h \ gimpbezierstroke.c \ gimpstroke.h \ diff --git a/app/vectors/gimpbezierdesc.c b/app/vectors/gimpbezierdesc.c new file mode 100644 index 0000000000..8d52f631a4 --- /dev/null +++ b/app/vectors/gimpbezierdesc.c @@ -0,0 +1,79 @@ +/* GIMP - The GNU Image Manipulation Program + * Copyright (C) 1995 Spencer Kimball and Peter Mattis + * + * gimpbezierdesc.c + * Copyright (C) 2010 Michael Natterer + * + * 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 . + */ + +#include "config.h" + +#include +#include + +#include "vectors-types.h" + +#include "gimpbezierdesc.h" + + +GimpBezierDesc * +gimp_bezier_desc_new (cairo_path_data_t *data, + gint n_data) +{ + GimpBezierDesc *desc; + + g_return_val_if_fail (n_data == 0 || data != NULL, NULL); + + desc = g_slice_new (GimpBezierDesc); + + desc->status = CAIRO_STATUS_SUCCESS; + desc->num_data = n_data; + desc->data = data; + + return desc; +} + +GimpBezierDesc * +gimp_bezier_desc_copy (const GimpBezierDesc *desc) +{ + g_return_val_if_fail (desc != NULL, NULL); + + return gimp_bezier_desc_new (g_memdup (desc->data, + desc->num_data * sizeof (cairo_path_data_t)), + desc->num_data); +} + +cairo_path_data_t * +gimp_bezier_desc_free (GimpBezierDesc *desc, + gboolean free_data) +{ + cairo_path_data_t *data; + + g_return_val_if_fail (desc != NULL, NULL); + + if (free_data) + { + g_free (desc->data); + data = NULL; + } + else + { + data = desc->data; + } + + g_slice_free (GimpBezierDesc, desc); + + return data; +} diff --git a/app/vectors/gimpbezierdesc.h b/app/vectors/gimpbezierdesc.h new file mode 100644 index 0000000000..fbcacb9b47 --- /dev/null +++ b/app/vectors/gimpbezierdesc.h @@ -0,0 +1,33 @@ +/* GIMP - The GNU Image Manipulation Program + * Copyright (C) 1995 Spencer Kimball and Peter Mattis + * + * gimpbezierdesc.h + * Copyright (C) 2010 Michael Natterer + * + * 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 . + */ + +#ifndef __GIMP_BEZIER_DESC_H__ +#define __GIMP_BEZIER_DESC_H__ + + +/* takes ownership of "data" */ +GimpBezierDesc * gimp_bezier_desc_new (cairo_path_data_t *data, + gint n_data); +GimpBezierDesc * gimp_bezier_desc_copy (const GimpBezierDesc *desc); +cairo_path_data_t * gimp_bezier_desc_free (GimpBezierDesc *desc, + gboolean free_data); + + +#endif /* __GIMP_BEZIER_DESC_H__ */ diff --git a/app/vectors/gimpbezierstroke.c b/app/vectors/gimpbezierstroke.c index 5ba0e63478..3efdfbee14 100644 --- a/app/vectors/gimpbezierstroke.c +++ b/app/vectors/gimpbezierstroke.c @@ -31,6 +31,7 @@ #include "core/gimpcoords-interpolate.h" #include "gimpanchor.h" +#include "gimpbezierdesc.h" #include "gimpbezierstroke.h" @@ -1535,11 +1536,8 @@ gimp_bezier_stroke_make_bezier (const GimpStroke *stroke) g_printerr ("miscalculated path cmd length! (%d vs. %d)\n", cmd_array->len, num_cmds); - bezdesc = g_slice_new (GimpBezierDesc); - bezdesc->status = CAIRO_STATUS_SUCCESS; - bezdesc->data = (cairo_path_data_t *) cmd_array->data; - bezdesc->num_data = cmd_array->len; - + bezdesc = gimp_bezier_desc_new ((cairo_path_data_t *) cmd_array->data, + cmd_array->len); g_array_free (points, TRUE); g_array_free (cmd_array, FALSE); diff --git a/app/vectors/gimpvectors.c b/app/vectors/gimpvectors.c index 8620ead49c..d578ed6a90 100644 --- a/app/vectors/gimpvectors.c +++ b/app/vectors/gimpvectors.c @@ -44,6 +44,7 @@ #include "paint/gimppaintoptions.h" #include "gimpanchor.h" +#include "gimpbezierdesc.c" #include "gimpstroke.h" #include "gimpvectors.h" #include "gimpvectors-preview.h" @@ -260,8 +261,7 @@ gimp_vectors_finalize (GObject *object) if (vectors->bezier_desc) { - g_free (vectors->bezier_desc->data); - g_slice_free (GimpBezierDesc, vectors->bezier_desc); + gimp_bezier_desc_free (vectors->bezier_desc, TRUE); vectors->bezier_desc = NULL; } @@ -613,8 +613,7 @@ gimp_vectors_real_freeze (GimpVectors *vectors) /* release cached bezier representation */ if (vectors->bezier_desc) { - g_free (vectors->bezier_desc->data); - g_slice_free (GimpBezierDesc, vectors->bezier_desc); + gimp_bezier_desc_free (vectors->bezier_desc, TRUE); vectors->bezier_desc = NULL; } @@ -1159,17 +1158,14 @@ gimp_vectors_real_make_bezier (const GimpVectors *vectors) { cmd_array = g_array_append_vals (cmd_array, bezdesc->data, bezdesc->num_data); - g_free (bezdesc->data); - g_slice_free (GimpBezierDesc, bezdesc); + gimp_bezier_desc_free (bezdesc, TRUE); } } if (cmd_array->len > 0) { - ret_bezdesc = g_slice_new (GimpBezierDesc); - ret_bezdesc->status = CAIRO_STATUS_SUCCESS; - ret_bezdesc->num_data = cmd_array->len; - ret_bezdesc->data = (cairo_path_data_t *) cmd_array->data; + ret_bezdesc = gimp_bezier_desc_new ((cairo_path_data_t *) cmd_array->data, + cmd_array->len); g_array_free (cmd_array, FALSE); }