gimpparasiteio.c 4.96 KB
Newer Older
1
/* LIBGIMP - The GIMP Library
2
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
3
 *
4
 * gimpparasiteio.c
5 6
 * Copyright (C) 1999 Tor Lillqvist <tml@iki.fi>
 *
7
 * This library is free software: you can redistribute it and/or
Marc Lehmann's avatar
Marc Lehmann committed
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 3 of the License, or (at your option) any later version.
11 12 13 14
 *
 * This library 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
15
 * Lesser General Public License for more details.
16
 *
Marc Lehmann's avatar
Marc Lehmann committed
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with this library.  If not, see
19
 * <https://www.gnu.org/licenses/>.
20 21
 */

22
/*
23
 * Functions for building and parsing string representations of
24 25 26
 * various standard parasite types.
 */

27 28 29
#include "config.h"

#include <stdio.h>
30
#include <stdlib.h>
31
#include <string.h>
32

33 34
#include <glib.h>

35
#include "gimpparasiteio.h"
36

37

38 39 40 41 42 43 44 45 46 47 48 49
/**
 * SECTION: gimpparasiteio
 * @title: gimpparasiteio
 * @short_description: Utility functions to (de)serialize certain C
 *                     structures to/from #GimpParasite's.
 * @see_also: #GimpParasite
 *
 * Utility functions to (de)serialize certain C structures to/from*
 * #GimpParasite's.
 **/


50
void
51
gimp_pixpipe_params_init (GimpPixPipeParams *params)
52
{
53
  gint i;
54

55 56
  g_return_if_fail (params != NULL);

57 58 59
  params->step       = 100;
  params->ncells     = 1;
  params->cellwidth  = 1;
60
  params->cellheight = 1;
61 62 63
  params->dim        = 1;
  params->cols       = 1;
  params->rows       = 1;
64 65
  params->placement  = g_strdup ("constant");

66
  for (i = 0; i < GIMP_PIXPIPE_MAXDIM; i++)
67
    params->selection[i] = g_strdup ("random");
68

69
  params->rank[0] = 1;
70
  for (i = 1; i < GIMP_PIXPIPE_MAXDIM; i++)
71 72 73 74
    params->rank[i] = 0;
}

void
75
gimp_pixpipe_params_parse (const gchar       *string,
Sven Neumann's avatar
Sven Neumann committed
76
                           GimpPixPipeParams *params)
77
{
78
  gchar *copy;
79 80
  gchar *p, *q, *r;
  gint   i;
81

82 83 84 85 86 87
  g_return_if_fail (string != NULL);
  g_return_if_fail (params != NULL);

  copy = g_strdup (string);

  q = copy;
88 89 90 91 92
  while ((p = strtok (q, " \r\n")) != NULL)
    {
      q = NULL;
      r = strchr (p, ':');
      if (r)
Sven Neumann's avatar
Sven Neumann committed
93
        *r = 0;
94 95

      if (strcmp (p, "ncells") == 0)
Sven Neumann's avatar
Sven Neumann committed
96 97 98 99
        {
          if (r)
            params->ncells = atoi (r + 1);
        }
100
      else if (strcmp (p, "step") == 0)
Sven Neumann's avatar
Sven Neumann committed
101 102 103 104
        {
          if (r)
            params->step = atoi (r + 1);
        }
105
      else if (strcmp (p, "dim") == 0)
Sven Neumann's avatar
Sven Neumann committed
106 107
        {
          if (r)
108 109 110 111 112
            {
              params->dim = atoi (r + 1);
              params->dim = CLAMP (params->dim, 1, GIMP_PIXPIPE_MAXDIM);
            }
        }
113
      else if (strcmp (p, "cols") == 0)
Sven Neumann's avatar
Sven Neumann committed
114 115 116 117
        {
          if (r)
            params->cols = atoi (r + 1);
        }
118
      else if (strcmp (p, "rows") == 0)
Sven Neumann's avatar
Sven Neumann committed
119 120 121 122
        {
          if (r)
            params->rows = atoi (r + 1);
        }
123
      else if (strcmp (p, "cellwidth") == 0)
Sven Neumann's avatar
Sven Neumann committed
124 125 126 127
        {
          if (r)
            params->cellwidth = atoi (r + 1);
        }
128
      else if (strcmp (p, "cellheight") == 0)
Sven Neumann's avatar
Sven Neumann committed
129 130 131 132
        {
          if (r)
            params->cellheight = atoi (r + 1);
        }
133
      else if (strcmp (p, "placement") == 0)
Sven Neumann's avatar
Sven Neumann committed
134 135 136
        {
          if (r)
            {
137
              g_free (params->placement);
Sven Neumann's avatar
Sven Neumann committed
138 139 140
              params->placement = g_strdup (r + 1);
            }
        }
141
      else if (strncmp (p, "rank", strlen ("rank")) == 0 && r)
Sven Neumann's avatar
Sven Neumann committed
142 143 144 145 146 147 148 149
        {
          if (r)
            {
              i = atoi (p + strlen ("rank"));
              if (i >= 0 && i < params->dim)
                params->rank[i] = atoi (r + 1);
            }
        }
150
      else if (strncmp (p, "sel", strlen ("sel")) == 0 && r)
Sven Neumann's avatar
Sven Neumann committed
151 152 153 154 155 156
        {
          if (r)
            {
              i = atoi (p + strlen ("sel"));
              if (i >= 0 && i < params->dim)
                {
157
                  g_free (params->selection[i]);
Sven Neumann's avatar
Sven Neumann committed
158 159 160 161
                  params->selection[i] = g_strdup (r + 1);
                }
            }
        }
162
      if (r)
Sven Neumann's avatar
Sven Neumann committed
163
        *r = ':';
164
    }
165 166

  g_free (copy);
167 168 169
}

gchar *
170
gimp_pixpipe_params_build (GimpPixPipeParams *params)
171
{
172
  GString *str;
173
  gint     i;
174

175 176
  g_return_val_if_fail (params != NULL, NULL);

177
  str = g_string_new (NULL);
178

179
  g_string_printf (str, "ncells:%d cellwidth:%d cellheight:%d "
180 181 182 183 184
                   "step:%d dim:%d cols:%d rows:%d placement:%s",
                   params->ncells, params->cellwidth, params->cellheight,
                   params->step, params->dim,
                   params->cols, params->rows,
                   params->placement);
185 186 187

  for (i = 0; i < params->dim; i++)
    {
188 189
      g_string_append_printf (str, " rank%d:%d", i, params->rank[i]);
      g_string_append_printf (str, " sel%d:%s", i, params->selection[i]);
190 191
    }

192
  return g_string_free (str, FALSE);
193
}
194 195 196 197 198 199

void
gimp_pixpipe_params_free (GimpPixPipeParams *params)
{
  gint i;

200 201
  g_free (params->placement);

202 203 204
  for (i = 0; i < GIMP_PIXPIPE_MAXDIM; i++)
    g_free (params->selection[i]);
}