1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
3
* Copyright (C) 1999 Adrian Likins and Tor Lillqvist
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 of the License, or
8
* (at your option) any later version.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
#include <sys/types.h>
37
#include <glib-object.h>
38
#include <glib/gstdio.h>
44
#include "libgimpbase/gimpbase.h"
45
#include "libgimpbase/gimpparasiteio.h"
47
#include "core-types.h"
49
#include "base/temp-buf.h"
51
#include "gimpbrush-load.h"
52
#include "gimpbrushpipe.h"
53
#include "gimpbrushpipe-load.h"
55
#include "gimp-intl.h"
59
gimp_brush_pipe_load (const gchar *filename,
62
GimpBrushPipe *pipe = NULL;
63
GimpPixPipeParams params;
65
gint num_of_brushes = 0;
72
g_return_val_if_fail (filename != NULL, NULL);
73
g_return_val_if_fail (g_path_is_absolute (filename), NULL);
74
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
76
fd = g_open (filename, O_RDONLY | _O_BINARY, 0);
80
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
81
_("Could not open '%s' for reading: %s"),
82
gimp_filename_to_utf8 (filename), g_strerror (errno));
86
/* The file format starts with a painfully simple text header */
89
buffer = g_string_new (NULL);
90
while (read (fd, &c, 1) == 1 && c != '\n' && buffer->len < 1024)
91
g_string_append_c (buffer, c);
93
if (buffer->len > 0 && buffer->len < 1024)
96
gimp_any_to_utf8 (buffer->str, buffer->len,
97
_("Invalid UTF-8 string in brush file '%s'."),
98
gimp_filename_to_utf8 (filename));
100
pipe = g_object_new (GIMP_TYPE_BRUSH_PIPE,
102
"mime-type", "image/x-gimp-gih",
108
g_string_free (buffer, TRUE);
112
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
113
_("Fatal parse error in brush file '%s': "
115
gimp_filename_to_utf8 (filename));
120
/* get the number of brushes */
121
buffer = g_string_new (NULL);
122
while (read (fd, &c, 1) == 1 && c != '\n' && buffer->len < 1024)
123
g_string_append_c (buffer, c);
125
if (buffer->len > 0 && buffer->len < 1024)
127
num_of_brushes = strtol (buffer->str, ¶mstring, 10);
130
if (num_of_brushes < 1)
132
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
133
_("Fatal parse error in brush file '%s': "
135
gimp_filename_to_utf8 (filename));
137
g_object_unref (pipe);
138
g_string_free (buffer, TRUE);
142
while (*paramstring && g_ascii_isspace (*paramstring))
147
gimp_pixpipe_params_init (¶ms);
148
gimp_pixpipe_params_parse (paramstring, ¶ms);
150
pipe->dimension = params.dim;
151
pipe->rank = g_new0 (gint, pipe->dimension);
152
pipe->select = g_new0 (PipeSelectModes, pipe->dimension);
153
pipe->index = g_new0 (gint, pipe->dimension);
155
/* placement is not used at all ?? */
156
if (params.free_placement_string)
157
g_free (params.placement);
159
for (i = 0; i < pipe->dimension; i++)
161
pipe->rank[i] = MAX (1, params.rank[i]);
162
if (strcmp (params.selection[i], "incremental") == 0)
163
pipe->select[i] = PIPE_SELECT_INCREMENTAL;
164
else if (strcmp (params.selection[i], "angular") == 0)
165
pipe->select[i] = PIPE_SELECT_ANGULAR;
166
else if (strcmp (params.selection[i], "velocity") == 0)
167
pipe->select[i] = PIPE_SELECT_VELOCITY;
168
else if (strcmp (params.selection[i], "random") == 0)
169
pipe->select[i] = PIPE_SELECT_RANDOM;
170
else if (strcmp (params.selection[i], "pressure") == 0)
171
pipe->select[i] = PIPE_SELECT_PRESSURE;
172
else if (strcmp (params.selection[i], "xtilt") == 0)
173
pipe->select[i] = PIPE_SELECT_TILT_X;
174
else if (strcmp (params.selection[i], "ytilt") == 0)
175
pipe->select[i] = PIPE_SELECT_TILT_Y;
177
pipe->select[i] = PIPE_SELECT_CONSTANT;
178
if (params.free_selection_string)
179
g_free (params.selection[i]);
186
pipe->rank = g_new (gint, 1);
187
pipe->rank[0] = num_of_brushes;
188
pipe->select = g_new (PipeSelectModes, 1);
189
pipe->select[0] = PIPE_SELECT_INCREMENTAL;
190
pipe->index = g_new (gint, 1);
194
g_string_free (buffer, TRUE);
196
totalcells = 1; /* Not all necessarily present, maybe */
197
for (i = 0; i < pipe->dimension; i++)
198
totalcells *= pipe->rank[i];
199
pipe->stride = g_new0 (gint, pipe->dimension);
200
for (i = 0; i < pipe->dimension; i++)
203
pipe->stride[i] = totalcells / pipe->rank[i];
205
pipe->stride[i] = pipe->stride[i-1] / pipe->rank[i];
207
g_assert (pipe->stride[pipe->dimension-1] == 1);
209
pipe->brushes = g_new0 (GimpBrush *, num_of_brushes);
211
while (pipe->nbrushes < num_of_brushes)
213
pipe->brushes[pipe->nbrushes] = gimp_brush_load_brush (fd, filename, NULL);
215
if (pipe->brushes[pipe->nbrushes])
217
gimp_object_set_name (GIMP_OBJECT (pipe->brushes[pipe->nbrushes]),
222
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
223
_("Fatal parse error in brush file '%s': "
225
gimp_filename_to_utf8 (filename));
227
g_object_unref (pipe);
236
/* Current brush is the first one. */
237
pipe->current = pipe->brushes[0];
239
/* just to satisfy the code that relies on this crap */
240
GIMP_BRUSH (pipe)->spacing = pipe->current->spacing;
241
GIMP_BRUSH (pipe)->x_axis = pipe->current->x_axis;
242
GIMP_BRUSH (pipe)->y_axis = pipe->current->y_axis;
243
GIMP_BRUSH (pipe)->mask = pipe->current->mask;
244
GIMP_BRUSH (pipe)->pixmap = pipe->current->pixmap;
246
return g_list_prepend (NULL, pipe);