~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to app/core/gimpbrushpipe-load.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
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 of the License, or
 
8
 * (at your option) 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
 
 
22
#include <stdlib.h>
 
23
#include <string.h>
 
24
#include <errno.h>
 
25
 
 
26
#include <sys/types.h>
 
27
 
 
28
#ifdef HAVE_UNISTD_H
 
29
#include <unistd.h>
 
30
#endif
 
31
#include <fcntl.h>
 
32
 
 
33
#ifndef _O_BINARY
 
34
#define _O_BINARY 0
 
35
#endif
 
36
 
 
37
#include <glib-object.h>
 
38
#include <glib/gstdio.h>
 
39
 
 
40
#ifdef G_OS_WIN32
 
41
#include <io.h>
 
42
#endif
 
43
 
 
44
#include "libgimpbase/gimpbase.h"
 
45
#include "libgimpbase/gimpparasiteio.h"
 
46
 
 
47
#include "core-types.h"
 
48
 
 
49
#include "base/temp-buf.h"
 
50
 
 
51
#include "gimpbrush-load.h"
 
52
#include "gimpbrushpipe.h"
 
53
#include "gimpbrushpipe-load.h"
 
54
 
 
55
#include "gimp-intl.h"
 
56
 
 
57
 
 
58
GList *
 
59
gimp_brush_pipe_load (const gchar  *filename,
 
60
                      GError      **error)
 
61
{
 
62
  GimpBrushPipe     *pipe = NULL;
 
63
  GimpPixPipeParams  params;
 
64
  gint               i;
 
65
  gint               num_of_brushes = 0;
 
66
  gint               totalcells;
 
67
  gchar             *paramstring;
 
68
  GString           *buffer;
 
69
  gchar              c;
 
70
  gint               fd;
 
71
 
 
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);
 
75
 
 
76
  fd = g_open (filename, O_RDONLY | _O_BINARY, 0);
 
77
 
 
78
  if (fd == -1)
 
79
    {
 
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));
 
83
      return NULL;
 
84
    }
 
85
 
 
86
  /* The file format starts with a painfully simple text header */
 
87
 
 
88
  /*  get the name  */
 
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);
 
92
 
 
93
  if (buffer->len > 0 && buffer->len < 1024)
 
94
    {
 
95
      gchar *utf8 =
 
96
        gimp_any_to_utf8 (buffer->str, buffer->len,
 
97
                          _("Invalid UTF-8 string in brush file '%s'."),
 
98
                          gimp_filename_to_utf8 (filename));
 
99
 
 
100
      pipe = g_object_new (GIMP_TYPE_BRUSH_PIPE,
 
101
                           "name",      utf8,
 
102
                           "mime-type", "image/x-gimp-gih",
 
103
                           NULL);
 
104
 
 
105
      g_free (utf8);
 
106
    }
 
107
 
 
108
  g_string_free (buffer, TRUE);
 
109
 
 
110
  if (! pipe)
 
111
    {
 
112
      g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
 
113
                   _("Fatal parse error in brush file '%s': "
 
114
                     "File is corrupt."),
 
115
                   gimp_filename_to_utf8 (filename));
 
116
      close (fd);
 
117
      return NULL;
 
118
    }
 
119
 
 
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);
 
124
 
 
125
  if (buffer->len > 0 && buffer->len < 1024)
 
126
    {
 
127
      num_of_brushes = strtol (buffer->str, &paramstring, 10);
 
128
    }
 
129
 
 
130
  if (num_of_brushes < 1)
 
131
    {
 
132
      g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
 
133
                   _("Fatal parse error in brush file '%s': "
 
134
                     "File is corrupt."),
 
135
                   gimp_filename_to_utf8 (filename));
 
136
      close (fd);
 
137
      g_object_unref (pipe);
 
138
      g_string_free (buffer, TRUE);
 
139
      return NULL;
 
140
    }
 
141
 
 
142
  while (*paramstring && g_ascii_isspace (*paramstring))
 
143
    paramstring++;
 
144
 
 
145
  if (*paramstring)
 
146
    {
 
147
      gimp_pixpipe_params_init (&params);
 
148
      gimp_pixpipe_params_parse (paramstring, &params);
 
149
 
 
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);
 
154
 
 
155
      /* placement is not used at all ?? */
 
156
      if (params.free_placement_string)
 
157
        g_free (params.placement);
 
158
 
 
159
      for (i = 0; i < pipe->dimension; i++)
 
160
        {
 
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;
 
176
          else
 
177
            pipe->select[i] = PIPE_SELECT_CONSTANT;
 
178
          if (params.free_selection_string)
 
179
            g_free (params.selection[i]);
 
180
          pipe->index[i] = 0;
 
181
        }
 
182
    }
 
183
  else
 
184
    {
 
185
      pipe->dimension = 1;
 
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);
 
191
      pipe->index[0]  = 0;
 
192
    }
 
193
 
 
194
  g_string_free (buffer, TRUE);
 
195
 
 
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++)
 
201
    {
 
202
      if (i == 0)
 
203
        pipe->stride[i] = totalcells / pipe->rank[i];
 
204
      else
 
205
        pipe->stride[i] = pipe->stride[i-1] / pipe->rank[i];
 
206
    }
 
207
  g_assert (pipe->stride[pipe->dimension-1] == 1);
 
208
 
 
209
  pipe->brushes = g_new0 (GimpBrush *, num_of_brushes);
 
210
 
 
211
  while (pipe->nbrushes < num_of_brushes)
 
212
    {
 
213
      pipe->brushes[pipe->nbrushes] = gimp_brush_load_brush (fd, filename, NULL);
 
214
 
 
215
      if (pipe->brushes[pipe->nbrushes])
 
216
        {
 
217
          gimp_object_set_name (GIMP_OBJECT (pipe->brushes[pipe->nbrushes]),
 
218
                                NULL);
 
219
        }
 
220
      else
 
221
        {
 
222
          g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
 
223
                       _("Fatal parse error in brush file '%s': "
 
224
                         "File is corrupt."),
 
225
                       gimp_filename_to_utf8 (filename));
 
226
          close (fd);
 
227
          g_object_unref (pipe);
 
228
          return NULL;
 
229
        }
 
230
 
 
231
      pipe->nbrushes++;
 
232
    }
 
233
 
 
234
  close (fd);
 
235
 
 
236
  /* Current brush is the first one. */
 
237
  pipe->current = pipe->brushes[0];
 
238
 
 
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;
 
245
 
 
246
  return g_list_prepend (NULL, pipe);
 
247
}