Ejemplo n.º 1
0
static GimpGradientSegment *
svg_parser_gradient_segments (GList *stops)
{
  GimpGradientSegment *segment;
  SvgStop             *stop;
  GList               *list;

  if (! stops)
    return NULL;

  stop = stops->data;

  segment = gimp_gradient_segment_new ();

  segment->left_color  = stop->color;
  segment->right_color = stop->color;

  /*  the list of offsets is sorted from largest to smallest  */
  for (list = g_list_next (stops); list; list = g_list_next (list))
    {
      GimpGradientSegment *next = segment;

      segment->left   = stop->offset;
      segment->middle = (segment->left + segment->right) / 2.0;

      segment = gimp_gradient_segment_new ();

      segment->next = next;
      next->prev    = segment;

      segment->right       = stop->offset;
      segment->right_color = stop->color;

      stop = list->data;

      segment->left_color  = stop->color;
    }

  segment->middle = (segment->left + segment->right) / 2.0;

  if (stop->offset > 0.0)
    segment->right_color = stop->color;

  /*  FIXME: remove empty segments here or add a GimpGradient API to do that
   */

  return segment;
}
static GimpGradientSegment *
gradient_editor_save_selection (GimpGradientEditor *editor)
{
  GimpGradientSegment *seg, *prev, *tmp;
  GimpGradientSegment *oseg, *oaseg;

  prev = NULL;
  oseg = editor->control_sel_l;
  tmp  = NULL;

  do
    {
      seg = gimp_gradient_segment_new ();

      *seg = *oseg; /* Copy everything */

      if (prev == NULL)
        tmp = seg; /* Remember first segment */
      else
        prev->next = seg;

      seg->prev = prev;
      seg->next = NULL;

      prev  = seg;
      oaseg = oseg;
      oseg  = oseg->next;
    }
  while (oaseg != editor->control_sel_r);

  return tmp;
}
Ejemplo n.º 3
0
void
gimp_gradients_init (Gimp *gimp)
{
  GimpGradient *gradient;

  g_return_if_fail (GIMP_IS_GIMP (gimp));

  /* FG to BG (RGB) */
  gradient = gimp_gradients_add_gradient (gimp,
                                          _("FG to BG (RGB)"),
                                          FG_BG_RGB_KEY);
  gradient->segments->left_color_type  = GIMP_GRADIENT_COLOR_FOREGROUND;
  gradient->segments->right_color_type = GIMP_GRADIENT_COLOR_BACKGROUND;
  gimp_context_set_gradient (gimp->user_context, gradient);

  /* FG to BG (Hardedge) */
  gradient = gimp_gradients_add_gradient (gimp,
                                          _("FG to BG (Hardedge)"),
                                          FG_BG_HARDEDGE_KEY);
  gradient->segments->left                   = 0.00;
  gradient->segments->middle                 = 0.25;
  gradient->segments->right                  = 0.50;
  gradient->segments->left_color_type        = GIMP_GRADIENT_COLOR_FOREGROUND;
  gradient->segments->right_color_type       = GIMP_GRADIENT_COLOR_FOREGROUND;
  gradient->segments->next                   = gimp_gradient_segment_new ();
  gradient->segments->next->prev             = gradient->segments;
  gradient->segments->next->left             = 0.50;
  gradient->segments->next->middle           = 0.75;
  gradient->segments->next->right            = 1.00;
  gradient->segments->next->left_color_type  = GIMP_GRADIENT_COLOR_BACKGROUND;
  gradient->segments->next->right_color_type = GIMP_GRADIENT_COLOR_BACKGROUND;

  /* FG to BG (HSV counter-clockwise) */
  gradient = gimp_gradients_add_gradient (gimp,
                                          _("FG to BG (HSV counter-clockwise)"),
                                          FG_BG_HSV_CCW_KEY);
  gradient->segments->left_color_type  = GIMP_GRADIENT_COLOR_FOREGROUND;
  gradient->segments->right_color_type = GIMP_GRADIENT_COLOR_BACKGROUND;
  gradient->segments->color            = GIMP_GRADIENT_SEGMENT_HSV_CCW;

  /* FG to BG (HSV clockwise hue) */
  gradient = gimp_gradients_add_gradient (gimp,
                                          _("FG to BG (HSV clockwise hue)"),
                                          FG_BG_HSV_CW_KEY);
  gradient->segments->left_color_type  = GIMP_GRADIENT_COLOR_FOREGROUND;
  gradient->segments->right_color_type = GIMP_GRADIENT_COLOR_BACKGROUND;
  gradient->segments->color            = GIMP_GRADIENT_SEGMENT_HSV_CW;

  /* FG to Transparent */
  gradient = gimp_gradients_add_gradient (gimp,
                                          _("FG to Transparent"),
                                          FG_TRANSPARENT_KEY);
  gradient->segments->left_color_type  = GIMP_GRADIENT_COLOR_FOREGROUND;
  gradient->segments->right_color_type = GIMP_GRADIENT_COLOR_FOREGROUND_TRANSPARENT;
}
Ejemplo n.º 4
0
GList *
gimp_gradient_load (GimpContext  *context,
                    const gchar  *filename,
                    GError      **error)
{
  GimpGradient        *gradient;
  GimpGradientSegment *prev;
  gint                 num_segments;
  gint                 i;
  FILE                *file;
  gchar                line[1024];
  gint                 linenum;

  g_return_val_if_fail (filename != NULL, NULL);
  g_return_val_if_fail (g_path_is_absolute (filename), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  file = g_fopen (filename, "rb");

  if (!file)
    {
      g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
                   _("Could not open '%s' for reading: %s"),
                   gimp_filename_to_utf8 (filename), g_strerror (errno));
      return NULL;
    }

  linenum = 1;
  if (! fgets (line, sizeof (line), file))
    {
      g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
                   _("Fatal parse error in gradient file '%s': "
                     "Read error in line %d."),
                   gimp_filename_to_utf8 (filename), linenum);
      fclose (file);
      return NULL;
    }

  if (! g_str_has_prefix (line, "GIMP Gradient"))
    {
      g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
                   _("Fatal parse error in gradient file '%s': "
                     "Not a GIMP gradient file."),
                   gimp_filename_to_utf8 (filename));
      fclose (file);
      return NULL;
    }

  gradient = g_object_new (GIMP_TYPE_GRADIENT,
                           "mime-type", "application/x-gimp-gradient",
                           NULL);

  linenum++;
  if (! fgets (line, sizeof (line), file))
    {
      g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
                   _("Fatal parse error in gradient file '%s': "
                     "Read error in line %d."),
                   gimp_filename_to_utf8 (filename), linenum);
      fclose (file);
      g_object_unref (gradient);
      return NULL;
    }

  if (g_str_has_prefix (line, "Name: "))
    {
      gchar *utf8;

      utf8 = gimp_any_to_utf8 (g_strstrip (line + strlen ("Name: ")), -1,
                               _("Invalid UTF-8 string in gradient file '%s'."),
                               gimp_filename_to_utf8 (filename));
      gimp_object_take_name (GIMP_OBJECT (gradient), utf8);

      linenum++;
      if (! fgets (line, sizeof (line), file))
        {
          g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
                       _("Fatal parse error in gradient file '%s': "
                         "Read error in line %d."),
                       gimp_filename_to_utf8 (filename), linenum);
          fclose (file);
          g_object_unref (gradient);
          return NULL;
        }
    }
  else /* old gradient format */
    {
      gimp_object_take_name (GIMP_OBJECT (gradient),
                             g_filename_display_basename (filename));
    }

  num_segments = atoi (line);

  if (num_segments < 1)
    {
      g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
                   _("Fatal parse error in gradient file '%s': "
                     "File is corrupt in line %d."),
                   gimp_filename_to_utf8 (filename), linenum);
      g_object_unref (gradient);
      fclose (file);
      return NULL;
    }

  prev = NULL;

  for (i = 0; i < num_segments; i++)
    {
      GimpGradientSegment *seg;
      gchar               *end;
      gint                 color;
      gint                 type;
      gint                 left_color_type;
      gint                 right_color_type;

      seg = gimp_gradient_segment_new ();

      seg->prev = prev;

      if (prev)
        prev->next = seg;
      else
        gradient->segments = seg;

      linenum++;
      if (! fgets (line, sizeof (line), file))
        {
          g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
                       _("Fatal parse error in gradient file '%s': "
                         "Read error in line %d."),
                       gimp_filename_to_utf8 (filename), linenum);
          fclose (file);
          g_object_unref (gradient);
          return NULL;
        }

      seg->left = g_ascii_strtod (line, &end);
      if (end && errno != ERANGE)
        seg->middle = g_ascii_strtod (end, &end);
      if (end && errno != ERANGE)
        seg->right = g_ascii_strtod (end, &end);

      if (end && errno != ERANGE)
        seg->left_color.r = g_ascii_strtod (end, &end);
      if (end && errno != ERANGE)
        seg->left_color.g = g_ascii_strtod (end, &end);
      if (end && errno != ERANGE)
        seg->left_color.b = g_ascii_strtod (end, &end);
      if (end && errno != ERANGE)
        seg->left_color.a = g_ascii_strtod (end, &end);

      if (end && errno != ERANGE)
        seg->right_color.r = g_ascii_strtod (end, &end);
      if (end && errno != ERANGE)
        seg->right_color.g = g_ascii_strtod (end, &end);
      if (end && errno != ERANGE)
        seg->right_color.b = g_ascii_strtod (end, &end);
      if (end && errno != ERANGE)
        seg->right_color.a = g_ascii_strtod (end, &end);

      if (errno != ERANGE)
        {
          switch (sscanf (end, "%d %d %d %d",
                          &type, &color,
                          &left_color_type, &right_color_type))
            {
            case 4:
              seg->left_color_type  = (GimpGradientColor) left_color_type;
              seg->right_color_type = (GimpGradientColor) right_color_type;
              /* fall thru */

            case 2:
              seg->type  = (GimpGradientSegmentType) type;
              seg->color = (GimpGradientSegmentColor) color;
              break;

            default:
              g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
                           _("Fatal parse error in gradient file '%s': "
                             "Corrupt segment %d in line %d."),
                           gimp_filename_to_utf8 (filename), i, linenum);
              g_object_unref (gradient);
              fclose (file);
              return NULL;
            }
        }
      else
        {
          g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
                       _("Fatal parse error in gradient file '%s': "
                         "Corrupt segment %d in line %d."),
                       gimp_filename_to_utf8 (filename), i, linenum);
          g_object_unref (gradient);
          fclose (file);
          return NULL;
        }

      if ( (prev && (prev->right < seg->left))
           || (!prev && (0. < seg->left) ))
        {
          g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
                       _("Gradient file '%s' is corrupt: "
                         "Segments do not span the range 0-1."),
                       gimp_filename_to_utf8 (filename));
          g_object_unref (gradient);
          fclose (file);
          return NULL;
        }

      prev = seg;
    }

  if (prev->right < 1.0)
    {
      g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
                   _("Gradient file '%s' is corrupt: "
                     "Segments do not span the range 0-1."),
                   gimp_filename_to_utf8 (filename));
      g_object_unref (gradient);
      fclose (file);
      return NULL;
    }

  fclose (file);

  return g_list_prepend (NULL, gradient);
}