gimpgradient-save.c 8.08 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
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 3 of the License, or
7 8 9 10 11 12 13 14
 * (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
15
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 17 18 19
 */

#include "config.h"

20 21
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>
22 23 24 25 26 27 28 29 30 31 32 33

#include "libgimpbase/gimpbase.h"

#include "core-types.h"

#include "gimpgradient.h"
#include "gimpgradient-save.h"

#include "gimp-intl.h"


gboolean
34 35 36
gimp_gradient_save (GimpData       *data,
                    GOutputStream  *output,
                    GError        **error)
37 38
{
  GimpGradient        *gradient = GIMP_GRADIENT (data);
39
  GString             *string;
40 41 42 43 44 45 46 47
  GimpGradientSegment *seg;
  gint                 num_segments;

  /* File format is:
   *
   *   GIMP Gradient
   *   Name: name
   *   number_of_segments
48 49
   *   left middle right r0 g0 b0 a0 r1 g1 b1 a1 type coloring left_color_type
   *   left middle right r0 g0 b0 a0 r1 g1 b1 a1 type coloring right_color_type
50 51 52
   *   ...
   */

53
  string = g_string_new ("GIMP Gradient\n");
54

55 56
  g_string_append_printf (string, "Name: %s\n",
                          gimp_object_get_name (gradient));
57 58 59 60 61 62 63 64 65 66 67 68

  /* Count number of segments */
  num_segments = 0;
  seg          = gradient->segments;

  while (seg)
    {
      num_segments++;
      seg = seg->next;
    }

  /* Write rest of file */
69
  g_string_append_printf (string, "%d\n", num_segments);
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

  for (seg = gradient->segments; seg; seg = seg->next)
    {
      gchar buf[11][G_ASCII_DTOSTR_BUF_SIZE];

      g_ascii_formatd (buf[0],  G_ASCII_DTOSTR_BUF_SIZE,
                       "%f", seg->left);
      g_ascii_formatd (buf[1],  G_ASCII_DTOSTR_BUF_SIZE,
                       "%f", seg->middle);
      g_ascii_formatd (buf[2],  G_ASCII_DTOSTR_BUF_SIZE,
                       "%f", seg->right);
      g_ascii_formatd (buf[3],  G_ASCII_DTOSTR_BUF_SIZE,
                       "%f", seg->left_color.r);
      g_ascii_formatd (buf[4],  G_ASCII_DTOSTR_BUF_SIZE,
                       "%f", seg->left_color.g);
      g_ascii_formatd (buf[5],  G_ASCII_DTOSTR_BUF_SIZE,
                       "%f", seg->left_color.b);
      g_ascii_formatd (buf[6],  G_ASCII_DTOSTR_BUF_SIZE,
                       "%f", seg->left_color.a);
      g_ascii_formatd (buf[7],  G_ASCII_DTOSTR_BUF_SIZE,
                       "%f", seg->right_color.r);
      g_ascii_formatd (buf[8],  G_ASCII_DTOSTR_BUF_SIZE,
                       "%f", seg->right_color.g);
      g_ascii_formatd (buf[9],  G_ASCII_DTOSTR_BUF_SIZE,
                       "%f", seg->right_color.b);
      g_ascii_formatd (buf[10], G_ASCII_DTOSTR_BUF_SIZE,
                       "%f", seg->right_color.a);

98 99 100 101 102 103 104 105 106
      g_string_append_printf (string,
                              "%s %s %s %s %s %s %s %s %s %s %s %d %d %d %d\n",
                              buf[0], buf[1], buf[2],   /* left, middle, right */
                              buf[3], buf[4], buf[5], buf[6],  /* left color   */
                              buf[7], buf[8], buf[9], buf[10], /* right color  */
                              (gint) seg->type,
                              (gint) seg->color,
                              (gint) seg->left_color_type,
                              (gint) seg->right_color_type);
107 108
    }

109
  if (! g_output_stream_write_all (output, string->str, string->len,
110
                                   NULL, NULL, error))
111 112
    {
      g_string_free (string, TRUE);
113

114 115 116 117
      return FALSE;
    }

  g_string_free (string, TRUE);
118 119 120 121 122

  return TRUE;
}

gboolean
123
gimp_gradient_save_pov (GimpGradient  *gradient,
Michael Natterer's avatar
Michael Natterer committed
124
                        GFile         *file,
125
                        GError       **error)
126
{
127 128
  GOutputStream       *output;
  GString             *string;
129 130 131
  GimpGradientSegment *seg;
  gchar                buf[G_ASCII_DTOSTR_BUF_SIZE];
  gchar                color_buf[4][G_ASCII_DTOSTR_BUF_SIZE];
132
  GError              *my_error = NULL;
133 134

  g_return_val_if_fail (GIMP_IS_GRADIENT (gradient), FALSE);
Michael Natterer's avatar
Michael Natterer committed
135
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
136 137
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

138 139
  output = G_OUTPUT_STREAM (g_file_replace (file,
                                            NULL, FALSE, G_FILE_CREATE_NONE,
140
                                            NULL, error));
141
  if (! output)
142
    return FALSE;
143 144

  string = g_string_new ("/* color_map file created by GIMP */\n"
145
                         "/* https://www.gimp.org/          */\n"
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
                         "color_map {\n");

  for (seg = gradient->segments; seg; seg = seg->next)
    {
      /* Left */
      g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f",
                       seg->left);
      g_ascii_formatd (color_buf[0], G_ASCII_DTOSTR_BUF_SIZE, "%f",
                       seg->left_color.r);
      g_ascii_formatd (color_buf[1], G_ASCII_DTOSTR_BUF_SIZE, "%f",
                       seg->left_color.g);
      g_ascii_formatd (color_buf[2], G_ASCII_DTOSTR_BUF_SIZE, "%f",
                       seg->left_color.b);
      g_ascii_formatd (color_buf[3], G_ASCII_DTOSTR_BUF_SIZE, "%f",
                       1.0 - seg->left_color.a);

      g_string_append_printf (string,
                              "\t[%s color rgbt <%s, %s, %s, %s>]\n",
                              buf,
                              color_buf[0], color_buf[1],
                              color_buf[2], color_buf[3]);

      /* Middle */
      g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f",
                       seg->middle);
      g_ascii_formatd (color_buf[0], G_ASCII_DTOSTR_BUF_SIZE, "%f",
                       (seg->left_color.r + seg->right_color.r) / 2.0);
      g_ascii_formatd (color_buf[1], G_ASCII_DTOSTR_BUF_SIZE, "%f",
                       (seg->left_color.g + seg->right_color.g) / 2.0);
      g_ascii_formatd (color_buf[2], G_ASCII_DTOSTR_BUF_SIZE, "%f",
                       (seg->left_color.b + seg->right_color.b) / 2.0);
      g_ascii_formatd (color_buf[3], G_ASCII_DTOSTR_BUF_SIZE, "%f",
                       1.0 - (seg->left_color.a + seg->right_color.a) / 2.0);

      g_string_append_printf (string,
                              "\t[%s color rgbt <%s, %s, %s, %s>]\n",
                              buf,
                              color_buf[0], color_buf[1],
                              color_buf[2], color_buf[3]);

      /* Right */
      g_ascii_formatd (buf, G_ASCII_DTOSTR_BUF_SIZE, "%f",
                       seg->right);
      g_ascii_formatd (color_buf[0], G_ASCII_DTOSTR_BUF_SIZE, "%f",
                       seg->right_color.r);
      g_ascii_formatd (color_buf[1], G_ASCII_DTOSTR_BUF_SIZE, "%f",
                       seg->right_color.g);
      g_ascii_formatd (color_buf[2], G_ASCII_DTOSTR_BUF_SIZE, "%f",
                       seg->right_color.b);
      g_ascii_formatd (color_buf[3], G_ASCII_DTOSTR_BUF_SIZE, "%f",
                       1.0 - seg->right_color.a);

      g_string_append_printf (string,
                              "\t[%s color rgbt <%s, %s, %s, %s>]\n",
                              buf,
                              color_buf[0], color_buf[1],
                              color_buf[2], color_buf[3]);
    }

  g_string_append_printf (string, "} /* color_map */\n");

  if (! g_output_stream_write_all (output, string->str, string->len,
208
                                   NULL, NULL, &my_error))
209
    {
210 211 212 213 214 215 216 217
      g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_WRITE,
                   _("Writing POV file '%s' failed: %s"),
                   gimp_file_get_utf8_name (file),
                   my_error->message);
      g_clear_error (&my_error);
      g_string_free (string, TRUE);
      g_object_unref (output);
      return FALSE;
218 219
    }

220 221 222
  g_string_free (string, TRUE);
  g_object_unref (output);

223 224
  return TRUE;
}