1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* gimp_brush_generated module Copyright 1998 Jay Cox <jaycox@earthlink.net>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdlib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>
#include "libgimpbase/gimpbase.h"
#include "core-types.h"
#include "gimp-utils.h"
#include "gimpbrushgenerated.h"
#include "gimpbrushgenerated-load.h"
#include "gimp-intl.h"
GList *
gimp_brush_generated_load (GimpContext *context,
GFile *file,
GInputStream *input,
GError **error)
{
GimpBrush *brush;
GDataInputStream *data_input;
gchar *string;
gsize string_len;
gint linenum;
gchar *name = NULL;
GimpBrushGeneratedShape shape = GIMP_BRUSH_GENERATED_CIRCLE;
gboolean have_shape = FALSE;
gint spikes = 2;
gdouble spacing;
gdouble radius;
gdouble hardness;
gdouble aspect_ratio;
gdouble angle;
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
data_input = g_data_input_stream_new (input);
/* make sure the file we are reading is the right type */
linenum = 1;
string_len = 256;
string = gimp_data_input_stream_read_line_always (data_input, &string_len,
NULL, error);
if (! string)
goto failed;
if (! g_str_has_prefix (string, "GIMP-VBR"))
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Not a GIMP brush file."));
g_free (string);
goto failed;
}
g_free (string);
/* make sure we are reading a compatible version */
linenum++;
string_len = 256;
string = gimp_data_input_stream_read_line_always (data_input, &string_len,
NULL, error);
if (! string)
goto failed;
if (! g_str_has_prefix (string, "1.0"))
{
if (! g_str_has_prefix (string, "1.5"))
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Unknown GIMP brush version."));
g_free (string);
goto failed;
}
else
{
have_shape = TRUE;
}
}
g_free (string);
/* read name */
linenum++;
string_len = 256;
string = gimp_data_input_stream_read_line_always (data_input, &string_len,
NULL, error);
if (! string)
goto failed;
g_strstrip (string);
/* the empty string is not an allowed name */
if (strlen (string) < 1)
{
name = g_strdup (_("Untitled"));
}
else
{
name = gimp_any_to_utf8 (string, -1,
_("Invalid UTF-8 string in brush file '%s'."),
gimp_file_get_utf8_name (file));
}
g_free (string);
if (have_shape)
{
GEnumClass *enum_class;
GEnumValue *shape_val;
enum_class = g_type_class_peek (GIMP_TYPE_BRUSH_GENERATED_SHAPE);
/* read shape */
linenum++;
string_len = 256;
string = gimp_data_input_stream_read_line_always (data_input, &string_len,
NULL, error);
if (! string)
goto failed;
g_strstrip (string);
shape_val = g_enum_get_value_by_nick (enum_class, string);
if (! shape_val)
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Unknown GIMP brush shape."));
g_free (string);
goto failed;
}
g_free (string);
shape = shape_val->value;
}
/* read brush spacing */
linenum++;
string_len = 256;
string = gimp_data_input_stream_read_line_always (data_input, &string_len,
NULL, error);
if (! string)
goto failed;
if (! gimp_ascii_strtod (string, NULL, &spacing))
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Invalid brush spacing."));
g_free (string);
goto failed;
}
g_free (string);
/* read brush radius */
linenum++;
string_len = 256;
string = gimp_data_input_stream_read_line_always (data_input, &string_len,
NULL, error);
if (! string)
goto failed;
if (! gimp_ascii_strtod (string, NULL, &radius))
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Invalid brush radius."));
g_free (string);
goto failed;
}
g_free (string);
if (have_shape)
{
/* read number of spikes */
linenum++;
string_len = 256;
string = gimp_data_input_stream_read_line_always (data_input, &string_len,
NULL, error);
if (! string)
goto failed;
if (! gimp_ascii_strtoi (string, NULL, 10, &spikes) ||
spikes < 2 || spikes > 20)
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Invalid brush spike count."));
g_free (string);
goto failed;
}
g_free (string);
}
/* read brush hardness */
linenum++;
string_len = 256;
string = gimp_data_input_stream_read_line_always (data_input, &string_len,
NULL, error);
if (! string)
goto failed;
if (! gimp_ascii_strtod (string, NULL, &hardness))
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Invalid brush hardness."));
g_free (string);
goto failed;
}
g_free (string);
/* read brush aspect_ratio */
linenum++;
string_len = 256;
string = gimp_data_input_stream_read_line_always (data_input, &string_len,
NULL, error);
if (! string)
goto failed;
if (! gimp_ascii_strtod (string, NULL, &aspect_ratio))
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Invalid brush aspect ratio."));
g_free (string);
goto failed;
}
g_free (string);
/* read brush angle */
linenum++;
string_len = 256;
string = gimp_data_input_stream_read_line_always (data_input, &string_len,
NULL, error);
if (! string)
goto failed;
if (! gimp_ascii_strtod (string, NULL, &angle))
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
_("Invalid brush angle."));
g_free (string);
goto failed;
}
g_free (string);
g_object_unref (data_input);
brush = GIMP_BRUSH (gimp_brush_generated_new (name, shape, radius, spikes,
hardness, aspect_ratio, angle));
g_free (name);
gimp_brush_set_spacing (brush, spacing);
return g_list_prepend (NULL, brush);
failed:
g_object_unref (data_input);
if (name)
g_free (name);
g_prefix_error (error, _("In line %d of brush file: "), linenum);
return NULL;
}
|