2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
1998-07-09 13:31:06 +08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* gimp_brush_generated module Copyright 1998 Jay Cox <jaycox@earthlink.net>
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
1998-07-09 13:31:06 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
1998-07-09 13:31:06 +08:00
|
|
|
* (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
|
2009-01-18 06:28:01 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
1998-07-09 13:31:06 +08:00
|
|
|
*/
|
2000-09-29 20:00:00 +08:00
|
|
|
|
1999-02-21 07:20:54 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
2002-12-02 21:39:09 +08:00
|
|
|
#include <errno.h>
|
2004-08-02 01:20:00 +08:00
|
|
|
#include <stdlib.h>
|
1999-02-21 07:20:54 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
1999-10-27 02:27:27 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2013-10-15 07:58:39 +08:00
|
|
|
#include <gdk-pixbuf/gdk-pixbuf.h>
|
2005-02-07 09:24:22 +08:00
|
|
|
#include <glib/gstdio.h>
|
2013-10-15 07:58:39 +08:00
|
|
|
#include <gegl.h>
|
2000-12-17 05:37:03 +08:00
|
|
|
|
2004-01-19 09:54:11 +08:00
|
|
|
#include "libgimpbase/gimpbase.h"
|
2001-01-24 07:56:18 +08:00
|
|
|
|
2001-05-10 06:34:59 +08:00
|
|
|
#include "core-types.h"
|
2000-12-17 05:37:03 +08:00
|
|
|
|
1998-07-09 13:31:06 +08:00
|
|
|
#include "gimpbrushgenerated.h"
|
2006-10-03 20:29:07 +08:00
|
|
|
#include "gimpbrushgenerated-load.h"
|
1998-07-09 13:31:06 +08:00
|
|
|
|
2003-03-26 00:38:19 +08:00
|
|
|
#include "gimp-intl.h"
|
2002-12-02 21:39:09 +08:00
|
|
|
|
2000-12-29 23:22:01 +08:00
|
|
|
|
2004-07-27 03:00:22 +08:00
|
|
|
GList *
|
2010-04-11 19:12:41 +08:00
|
|
|
gimp_brush_generated_load (GimpContext *context,
|
2014-07-01 08:30:22 +08:00
|
|
|
GFile *file,
|
2002-12-02 21:39:09 +08:00
|
|
|
GError **error)
|
2001-02-11 09:39:24 +08:00
|
|
|
{
|
2005-11-01 22:41:41 +08:00
|
|
|
GimpBrush *brush;
|
2014-07-01 08:30:22 +08:00
|
|
|
gchar *path;
|
|
|
|
FILE *f;
|
2004-08-01 11:06:58 +08:00
|
|
|
gchar string[256];
|
2006-10-05 01:50:35 +08:00
|
|
|
gint linenum;
|
2004-08-10 19:33:13 +08:00
|
|
|
gchar *name = NULL;
|
|
|
|
GimpBrushGeneratedShape shape = GIMP_BRUSH_GENERATED_CIRCLE;
|
2004-08-01 11:06:58 +08:00
|
|
|
gboolean have_shape = FALSE;
|
2004-08-10 19:33:13 +08:00
|
|
|
gint spikes = 2;
|
2004-08-01 11:06:58 +08:00
|
|
|
gdouble spacing;
|
|
|
|
gdouble radius;
|
|
|
|
gdouble hardness;
|
|
|
|
gdouble aspect_ratio;
|
|
|
|
gdouble angle;
|
2001-02-11 09:39:24 +08:00
|
|
|
|
2014-07-01 08:30:22 +08:00
|
|
|
g_return_val_if_fail (G_IS_FILE (file), NULL);
|
|
|
|
|
|
|
|
path = g_file_get_path (file);
|
2002-12-02 21:39:09 +08:00
|
|
|
|
2014-07-01 08:30:22 +08:00
|
|
|
g_return_val_if_fail (g_path_is_absolute (path), NULL);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
2004-01-30 00:19:57 +08:00
|
|
|
|
2014-07-01 08:30:22 +08:00
|
|
|
f = g_fopen (path, "rb");
|
2014-07-01 20:25:37 +08:00
|
|
|
g_free (path);
|
|
|
|
|
2014-07-01 08:30:22 +08:00
|
|
|
if (! f)
|
2002-12-02 21:39:09 +08:00
|
|
|
{
|
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
|
|
|
|
_("Could not open '%s' for reading: %s"),
|
2014-07-01 20:25:37 +08:00
|
|
|
gimp_file_get_utf8_name (file), g_strerror (errno));
|
2002-12-02 21:39:09 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2001-02-11 09:39:24 +08:00
|
|
|
|
|
|
|
/* make sure the file we are reading is the right type */
|
2004-06-25 21:41:24 +08:00
|
|
|
errno = 0;
|
2006-10-05 01:50:35 +08:00
|
|
|
linenum = 1;
|
2014-07-01 08:30:22 +08:00
|
|
|
if (! fgets (string, sizeof (string), f))
|
2004-06-25 21:41:24 +08:00
|
|
|
goto failed;
|
2002-12-02 21:39:09 +08:00
|
|
|
|
2006-11-13 04:30:50 +08:00
|
|
|
if (! g_str_has_prefix (string, "GIMP-VBR"))
|
2002-12-02 21:39:09 +08:00
|
|
|
{
|
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
2003-11-14 23:33:40 +08:00
|
|
|
_("Fatal parse error in brush file '%s': "
|
|
|
|
"Not a GIMP brush file."),
|
2014-07-01 20:25:37 +08:00
|
|
|
gimp_file_get_utf8_name (file));
|
2004-08-10 19:33:13 +08:00
|
|
|
goto failed;
|
2002-12-02 21:39:09 +08:00
|
|
|
}
|
2001-02-14 22:57:14 +08:00
|
|
|
|
2001-02-11 09:39:24 +08:00
|
|
|
/* make sure we are reading a compatible version */
|
2004-06-25 21:41:24 +08:00
|
|
|
errno = 0;
|
2006-10-05 01:50:35 +08:00
|
|
|
linenum++;
|
2014-07-01 08:30:22 +08:00
|
|
|
if (! fgets (string, sizeof (string), f))
|
2004-06-25 21:41:24 +08:00
|
|
|
goto failed;
|
|
|
|
|
2006-11-13 04:30:50 +08:00
|
|
|
if (! g_str_has_prefix (string, "1.0"))
|
2002-12-02 21:39:09 +08:00
|
|
|
{
|
2006-11-13 04:30:50 +08:00
|
|
|
if (! g_str_has_prefix (string, "1.5"))
|
2004-08-01 11:06:58 +08:00
|
|
|
{
|
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
|
|
|
_("Fatal parse error in brush file '%s': "
|
2006-10-05 01:50:35 +08:00
|
|
|
"Unknown GIMP brush version in line %d."),
|
2014-07-01 20:25:37 +08:00
|
|
|
gimp_file_get_utf8_name (file), linenum);
|
2004-08-10 19:33:13 +08:00
|
|
|
goto failed;
|
2004-08-01 11:06:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
have_shape = TRUE;
|
|
|
|
}
|
2002-12-02 21:39:09 +08:00
|
|
|
}
|
2001-02-11 09:39:24 +08:00
|
|
|
|
2004-01-30 00:19:57 +08:00
|
|
|
/* read name */
|
2004-06-25 21:41:24 +08:00
|
|
|
errno = 0;
|
2006-10-05 01:50:35 +08:00
|
|
|
linenum++;
|
2014-07-01 08:30:22 +08:00
|
|
|
if (! fgets (string, sizeof (string), f))
|
2004-06-25 21:41:24 +08:00
|
|
|
goto failed;
|
2001-02-11 09:39:24 +08:00
|
|
|
|
2004-06-25 21:41:24 +08:00
|
|
|
g_strstrip (string);
|
2006-05-29 23:43:05 +08:00
|
|
|
|
|
|
|
/* the empty string is not an allowed name */
|
|
|
|
if (strlen (string) < 1)
|
|
|
|
g_strlcpy (string, _("Untitled"), sizeof (string));
|
|
|
|
|
2004-06-25 21:41:24 +08:00
|
|
|
name = gimp_any_to_utf8 (string, -1,
|
|
|
|
_("Invalid UTF-8 string in brush file '%s'."),
|
2014-07-01 20:25:37 +08:00
|
|
|
gimp_file_get_utf8_name (file));
|
2001-02-11 09:39:24 +08:00
|
|
|
|
2004-08-01 11:06:58 +08:00
|
|
|
if (have_shape)
|
|
|
|
{
|
|
|
|
GEnumClass *enum_class;
|
|
|
|
GEnumValue *shape_val;
|
|
|
|
|
|
|
|
enum_class = g_type_class_peek (GIMP_TYPE_BRUSH_GENERATED_SHAPE);
|
|
|
|
|
|
|
|
/* read shape */
|
|
|
|
errno = 0;
|
2006-10-05 01:50:35 +08:00
|
|
|
linenum++;
|
2014-07-01 08:30:22 +08:00
|
|
|
if (! fgets (string, sizeof (string), f))
|
2004-08-01 11:06:58 +08:00
|
|
|
goto failed;
|
|
|
|
|
|
|
|
g_strstrip (string);
|
|
|
|
shape_val = g_enum_get_value_by_nick (enum_class, string);
|
|
|
|
|
2006-10-05 01:50:35 +08:00
|
|
|
if (! shape_val)
|
2004-08-01 11:06:58 +08:00
|
|
|
{
|
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
|
|
|
_("Fatal parse error in brush file '%s': "
|
2006-10-05 01:50:35 +08:00
|
|
|
"Unknown GIMP brush shape in line %d."),
|
2014-07-01 20:25:37 +08:00
|
|
|
gimp_file_get_utf8_name (file), linenum);
|
2004-08-10 19:33:13 +08:00
|
|
|
goto failed;
|
2004-08-01 11:06:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
shape = shape_val->value;
|
|
|
|
}
|
|
|
|
|
2001-02-11 09:39:24 +08:00
|
|
|
/* read brush spacing */
|
2004-06-25 21:41:24 +08:00
|
|
|
errno = 0;
|
2006-10-05 01:50:35 +08:00
|
|
|
linenum++;
|
2014-07-01 08:30:22 +08:00
|
|
|
if (! fgets (string, sizeof (string), f))
|
2004-06-25 21:41:24 +08:00
|
|
|
goto failed;
|
|
|
|
spacing = g_ascii_strtod (string, NULL);
|
2001-02-11 09:39:24 +08:00
|
|
|
|
|
|
|
/* read brush radius */
|
2004-06-25 21:41:24 +08:00
|
|
|
errno = 0;
|
2006-10-05 01:50:35 +08:00
|
|
|
linenum++;
|
2014-07-01 08:30:22 +08:00
|
|
|
if (! fgets (string, sizeof (string), f))
|
2004-06-25 21:41:24 +08:00
|
|
|
goto failed;
|
|
|
|
radius = g_ascii_strtod (string, NULL);
|
2001-02-11 09:39:24 +08:00
|
|
|
|
2004-08-02 01:20:00 +08:00
|
|
|
if (have_shape)
|
|
|
|
{
|
2006-10-05 01:50:35 +08:00
|
|
|
/* read number of spikes */
|
2004-08-02 01:20:00 +08:00
|
|
|
errno = 0;
|
2006-10-05 01:50:35 +08:00
|
|
|
linenum++;
|
2014-07-01 08:30:22 +08:00
|
|
|
if (! fgets (string, sizeof (string), f))
|
2004-08-02 01:20:00 +08:00
|
|
|
goto failed;
|
|
|
|
spikes = CLAMP (atoi (string), 2, 20);
|
|
|
|
}
|
|
|
|
|
2001-02-11 09:39:24 +08:00
|
|
|
/* read brush hardness */
|
2004-06-25 21:41:24 +08:00
|
|
|
errno = 0;
|
2006-10-05 01:50:35 +08:00
|
|
|
linenum++;
|
2014-07-01 08:30:22 +08:00
|
|
|
if (! fgets (string, sizeof (string), f))
|
2004-06-25 21:41:24 +08:00
|
|
|
goto failed;
|
|
|
|
hardness = g_ascii_strtod (string, NULL);
|
2001-02-11 09:39:24 +08:00
|
|
|
|
|
|
|
/* read brush aspect_ratio */
|
2004-06-25 21:41:24 +08:00
|
|
|
errno = 0;
|
2006-10-05 01:50:35 +08:00
|
|
|
linenum++;
|
2014-07-01 08:30:22 +08:00
|
|
|
if (! fgets (string, sizeof (string), f))
|
2004-06-25 21:41:24 +08:00
|
|
|
goto failed;
|
|
|
|
aspect_ratio = g_ascii_strtod (string, NULL);
|
2001-02-11 09:39:24 +08:00
|
|
|
|
|
|
|
/* read brush angle */
|
2004-06-25 21:41:24 +08:00
|
|
|
errno = 0;
|
2006-10-05 01:50:35 +08:00
|
|
|
linenum++;
|
2014-07-01 08:30:22 +08:00
|
|
|
if (! fgets (string, sizeof (string), f))
|
2004-06-25 21:41:24 +08:00
|
|
|
goto failed;
|
|
|
|
angle = g_ascii_strtod (string, NULL);
|
2001-02-11 09:39:24 +08:00
|
|
|
|
2014-07-01 08:30:22 +08:00
|
|
|
fclose (f);
|
2001-02-11 09:39:24 +08:00
|
|
|
|
2005-11-01 22:41:41 +08:00
|
|
|
brush = GIMP_BRUSH (gimp_brush_generated_new (name, shape, radius, spikes,
|
|
|
|
hardness, aspect_ratio, angle));
|
2004-06-25 21:41:24 +08:00
|
|
|
g_free (name);
|
|
|
|
|
2005-11-01 22:41:41 +08:00
|
|
|
brush->spacing = spacing;
|
2004-06-25 21:41:24 +08:00
|
|
|
|
2004-07-27 03:00:22 +08:00
|
|
|
return g_list_prepend (NULL, brush);
|
2004-06-25 21:41:24 +08:00
|
|
|
|
|
|
|
failed:
|
|
|
|
|
2014-07-01 08:30:22 +08:00
|
|
|
fclose (f);
|
2004-08-10 10:37:44 +08:00
|
|
|
|
2004-06-25 21:41:24 +08:00
|
|
|
if (name)
|
|
|
|
g_free (name);
|
|
|
|
|
2004-08-10 19:33:13 +08:00
|
|
|
if (error && *error == NULL)
|
2006-10-05 01:50:35 +08:00
|
|
|
{
|
|
|
|
gchar *msg;
|
|
|
|
|
|
|
|
if (errno)
|
|
|
|
msg = g_strdup_printf (_("Line %d: %s"), linenum, g_strerror (errno));
|
|
|
|
else
|
|
|
|
msg = g_strdup_printf (_("File is truncated in line %d"), linenum);
|
|
|
|
|
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
|
|
|
_("Error while reading brush file '%s': %s"),
|
2014-07-01 20:25:37 +08:00
|
|
|
gimp_file_get_utf8_name (file), msg);
|
2006-10-05 01:50:35 +08:00
|
|
|
|
|
|
|
g_free (msg);
|
|
|
|
}
|
2004-06-25 21:41:24 +08:00
|
|
|
|
|
|
|
return NULL;
|
2001-02-11 09:39:24 +08:00
|
|
|
}
|