~ubuntu-branches/ubuntu/breezy/gimp/breezy

« back to all changes in this revision

Viewing changes to app/core/gimpgradient-save.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2005-10-04 19:04:46 UTC
  • Revision ID: james.westby@ubuntu.com-20051004190446-ukh32kwk56s4sjhu
Tags: upstream-2.2.8
ImportĀ upstreamĀ versionĀ 2.2.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
#include "config.h"
 
20
 
 
21
#include <stdio.h>
 
22
#include <string.h>
 
23
#include <errno.h>
 
24
 
 
25
#include <glib-object.h>
 
26
 
 
27
#include "libgimpbase/gimpbase.h"
 
28
 
 
29
#include "core-types.h"
 
30
 
 
31
#include "gimpgradient.h"
 
32
#include "gimpgradient-save.h"
 
33
 
 
34
#include "gimp-intl.h"
 
35
 
 
36
 
 
37
gboolean
 
38
gimp_gradient_save (GimpData  *data,
 
39
                    GError   **error)
 
40
{
 
41
  GimpGradient        *gradient = GIMP_GRADIENT (data);
 
42
  GimpGradientSegment *seg;
 
43
  gint                 num_segments;
 
44
  FILE                *file;
 
45
 
 
46
  file = fopen (data->filename, "wb");
 
47
 
 
48
  if (! file)
 
49
    {
 
50
      g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
 
51
                   _("Could not open '%s' for writing: %s"),
 
52
                   gimp_filename_to_utf8 (data->filename),
 
53
                   g_strerror (errno));
 
54
      return FALSE;
 
55
    }
 
56
 
 
57
  /* File format is:
 
58
   *
 
59
   *   GIMP Gradient
 
60
   *   Name: name
 
61
   *   number_of_segments
 
62
   *   left middle right r0 g0 b0 a0 r1 g1 b1 a1 type coloring
 
63
   *   left middle right r0 g0 b0 a0 r1 g1 b1 a1 type coloring
 
64
   *   ...
 
65
   */
 
66
 
 
67
  fprintf (file, "GIMP Gradient\n");
 
68
 
 
69
  fprintf (file, "Name: %s\n", GIMP_OBJECT (gradient)->name);
 
70
 
 
71
  /* Count number of segments */
 
72
  num_segments = 0;
 
73
  seg          = gradient->segments;
 
74
 
 
75
  while (seg)
 
76
    {
 
77
      num_segments++;
 
78
      seg = seg->next;
 
79
    }
 
80
 
 
81
  /* Write rest of file */
 
82
  fprintf (file, "%d\n", num_segments);
 
83
 
 
84
  for (seg = gradient->segments; seg; seg = seg->next)
 
85
    {
 
86
      gchar buf[11][G_ASCII_DTOSTR_BUF_SIZE];
 
87
 
 
88
      g_ascii_formatd (buf[0],  G_ASCII_DTOSTR_BUF_SIZE,
 
89
                       "%f", seg->left);
 
90
      g_ascii_formatd (buf[1],  G_ASCII_DTOSTR_BUF_SIZE,
 
91
                       "%f", seg->middle);
 
92
      g_ascii_formatd (buf[2],  G_ASCII_DTOSTR_BUF_SIZE,
 
93
                       "%f", seg->right);
 
94
      g_ascii_formatd (buf[3],  G_ASCII_DTOSTR_BUF_SIZE,
 
95
                       "%f", seg->left_color.r);
 
96
      g_ascii_formatd (buf[4],  G_ASCII_DTOSTR_BUF_SIZE,
 
97
                       "%f", seg->left_color.g);
 
98
      g_ascii_formatd (buf[5],  G_ASCII_DTOSTR_BUF_SIZE,
 
99
                       "%f", seg->left_color.b);
 
100
      g_ascii_formatd (buf[6],  G_ASCII_DTOSTR_BUF_SIZE,
 
101
                       "%f", seg->left_color.a);
 
102
      g_ascii_formatd (buf[7],  G_ASCII_DTOSTR_BUF_SIZE,
 
103
                       "%f", seg->right_color.r);
 
104
      g_ascii_formatd (buf[8],  G_ASCII_DTOSTR_BUF_SIZE,
 
105
                       "%f", seg->right_color.g);
 
106
      g_ascii_formatd (buf[9],  G_ASCII_DTOSTR_BUF_SIZE,
 
107
                       "%f", seg->right_color.b);
 
108
      g_ascii_formatd (buf[10], G_ASCII_DTOSTR_BUF_SIZE,
 
109
                       "%f", seg->right_color.a);
 
110
 
 
111
      fprintf (file, "%s %s %s %s %s %s %s %s %s %s %s %d %d\n",
 
112
               buf[0], buf[1], buf[2], buf[3], buf[4],
 
113
               buf[5], buf[6], buf[7], buf[8], buf[9], buf[10],
 
114
               (gint) seg->type,
 
115
               (gint) seg->color);
 
116
    }
 
117
 
 
118
  fclose (file);
 
119
 
 
120
  return TRUE;
 
121
}
 
122
 
 
123
gboolean
 
124
gimp_gradient_save_as_pov (GimpGradient  *gradient,
 
125
                           const gchar   *filename,
 
126
                           GError       **error)
 
127
{
 
128
  FILE                *file;
 
129
  GimpGradientSegment *seg;
 
130
  gchar                buf[G_ASCII_DTOSTR_BUF_SIZE];
 
131
  gchar                color_buf[4][G_ASCII_DTOSTR_BUF_SIZE];
 
132
 
 
133
  g_return_val_if_fail (GIMP_IS_GRADIENT (gradient), FALSE);
 
134
  g_return_val_if_fail (filename != NULL, FALSE);
 
135
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
136
 
 
137
  file = fopen (filename, "wb");
 
138
 
 
139
  if (! file)
 
140
    {
 
141
      g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
 
142
                   _("Could not open '%s' for writing: %s"),
 
143
                   gimp_filename_to_utf8 (filename), g_strerror (errno));
 
144
      return FALSE;
 
145
    }
 
146
  else
 
147
    {
 
148
      fprintf (file, "/* color_map file created by the GIMP */\n");
 
149
      fprintf (file, "/* http://www.gimp.org/               */\n");
 
150
 
 
151
      fprintf (file, "color_map {\n");
 
152
 
 
153
      for (seg = gradient->segments; seg; seg = seg->next)
 
154
        {
 
155
          /* Left */
 
156
          g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f",
 
157
                           seg->left);
 
158
          g_ascii_formatd (color_buf[0], G_ASCII_DTOSTR_BUF_SIZE, "%f",
 
159
                           seg->left_color.r);
 
160
          g_ascii_formatd (color_buf[1], G_ASCII_DTOSTR_BUF_SIZE, "%f",
 
161
                           seg->left_color.g);
 
162
          g_ascii_formatd (color_buf[2], G_ASCII_DTOSTR_BUF_SIZE, "%f",
 
163
                           seg->left_color.b);
 
164
          g_ascii_formatd (color_buf[3], G_ASCII_DTOSTR_BUF_SIZE, "%f",
 
165
                           1.0 - seg->left_color.a);
 
166
 
 
167
          fprintf (file, "\t[%s color rgbt <%s, %s, %s, %s>]\n",
 
168
                   buf,
 
169
                   color_buf[0], color_buf[1], color_buf[2], color_buf[3]);
 
170
 
 
171
          /* Middle */
 
172
          g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f",
 
173
                           seg->middle);
 
174
          g_ascii_formatd (color_buf[0], G_ASCII_DTOSTR_BUF_SIZE, "%f",
 
175
                           (seg->left_color.r + seg->right_color.r) / 2.0);
 
176
          g_ascii_formatd (color_buf[1], G_ASCII_DTOSTR_BUF_SIZE, "%f",
 
177
                           (seg->left_color.g + seg->right_color.g) / 2.0);
 
178
          g_ascii_formatd (color_buf[2], G_ASCII_DTOSTR_BUF_SIZE, "%f",
 
179
                           (seg->left_color.b + seg->right_color.b) / 2.0);
 
180
          g_ascii_formatd (color_buf[3], G_ASCII_DTOSTR_BUF_SIZE, "%f",
 
181
                           1.0 - (seg->left_color.a + seg->right_color.a) / 2.0);
 
182
 
 
183
          fprintf (file, "\t[%s color rgbt <%s, %s, %s, %s>]\n",
 
184
                   buf,
 
185
                   color_buf[0], color_buf[1], color_buf[2], color_buf[3]);
 
186
 
 
187
          /* Right */
 
188
          g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f",
 
189
                           seg->right);
 
190
          g_ascii_formatd (color_buf[0], G_ASCII_DTOSTR_BUF_SIZE, "%f",
 
191
                           seg->right_color.r);
 
192
          g_ascii_formatd (color_buf[1], G_ASCII_DTOSTR_BUF_SIZE, "%f",
 
193
                           seg->right_color.g);
 
194
          g_ascii_formatd (color_buf[2], G_ASCII_DTOSTR_BUF_SIZE, "%f",
 
195
                           seg->right_color.b);
 
196
          g_ascii_formatd (color_buf[3], G_ASCII_DTOSTR_BUF_SIZE, "%f",
 
197
                           1.0 - seg->right_color.a);
 
198
 
 
199
          fprintf (file, "\t[%s color rgbt <%s, %s, %s, %s>]\n",
 
200
                   buf,
 
201
                   color_buf[0], color_buf[1], color_buf[2], color_buf[3]);
 
202
        }
 
203
 
 
204
      fprintf (file, "} /* color_map */\n");
 
205
      fclose (file);
 
206
    }
 
207
 
 
208
  return TRUE;
 
209
}