Example #1
0
static void
palette_editor_edit_color_update (GimpColorDialog      *dialog,
                                  const GimpRGB        *color,
                                  GimpColorDialogState  state,
                                  GimpPaletteEditor    *editor)
{
  GimpPalette *palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data);

  switch (state)
    {
    case GIMP_COLOR_DIALOG_UPDATE:
      break;

    case GIMP_COLOR_DIALOG_OK:
      if (editor->color)
        {
          editor->color->color = *color;
          gimp_data_dirty (GIMP_DATA (palette));
        }
      /* Fallthrough */

    case GIMP_COLOR_DIALOG_CANCEL:
      gtk_widget_hide (editor->color_dialog);
      break;
    }
}
Example #2
0
gboolean
gimp_palette_editor_set_index (GimpPaletteEditor *editor,
                               gint               index,
                               GimpRGB           *color)
{
  GimpPalette *palette;

  g_return_val_if_fail (GIMP_IS_PALETTE_EDITOR (editor), FALSE);

  palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data);

  if (palette && gimp_palette_get_n_colors (palette) > 0)
    {
      GimpPaletteEntry *entry;

      index = CLAMP (index, 0, gimp_palette_get_n_colors (palette) - 1);

      entry = gimp_palette_get_entry (palette, index);

      gimp_palette_view_select_entry (GIMP_PALETTE_VIEW (editor->view),
                                      entry);

      if (color)
        *color = editor->color->color;

      return TRUE;
    }

  return FALSE;
}
Example #3
0
GList *
gimp_palette_load_act (GimpContext   *context,
                       GFile         *file,
                       GInputStream  *input,
                       GError       **error)
{
  GimpPalette *palette;
  gchar       *palette_name;
  guchar       color_bytes[3];
  gsize        bytes_read;

  g_return_val_if_fail (G_IS_FILE (file), NULL);
  g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  palette_name = g_path_get_basename (gimp_file_get_utf8_name (file));
  palette = GIMP_PALETTE (gimp_palette_new (context, palette_name));
  g_free (palette_name);

  while (g_input_stream_read_all (input, color_bytes, sizeof (color_bytes),
                                  &bytes_read, NULL, NULL) &&
         bytes_read == sizeof (color_bytes))
    {
      GimpRGB color;

      gimp_rgba_set_uchar (&color,
                           color_bytes[0],
                           color_bytes[1],
                           color_bytes[2],
                           255);
      gimp_palette_add_entry (palette, -1, NULL, &color);
    }

  return g_list_prepend (NULL, palette);
}
Example #4
0
static GimpPaletteEntry *
gimp_palette_view_find_entry (GimpPaletteView *view,
                              gint             x,
                              gint             y)
{
  GimpPalette             *palette;
  GimpViewRendererPalette *renderer;
  GimpPaletteEntry        *entry = NULL;
  gint                     col, row;

  palette  = GIMP_PALETTE (GIMP_VIEW (view)->renderer->viewable);
  renderer = GIMP_VIEW_RENDERER_PALETTE (GIMP_VIEW (view)->renderer);

  if (! palette)
    return NULL;

  col = x / renderer->cell_width;
  row = y / renderer->cell_height;

  if (col >= 0 && col < renderer->columns &&
      row >= 0 && row < renderer->rows)
    {
      entry = gimp_palette_get_entry (palette,
                                      row * renderer->columns + col);
    }

  return entry;
}
Example #5
0
static gboolean
convert_dialog_palette_filter (const GimpObject *object,
                               gpointer          user_data)
{
  GimpPalette *palette = GIMP_PALETTE (object);

  return palette->n_colors > 0 && palette->n_colors <= 256;
}
static void
palettes_merge_callback (GtkWidget   *widget,
                         const gchar *palette_name,
                         gpointer     data)
{
  /* FIXME: reimplement palettes_merge_callback() */
#if 0
  GimpContainerEditor *editor;
  GimpPalette         *palette;
  GimpPalette         *new_palette;
  GList               *sel_list;

  editor = (GimpContainerEditor *) data;

  sel_list = GTK_LIST (GIMP_CONTAINER_LIST_VIEW (editor->view)->gtk_list)->selection;

  if (! sel_list)
    {
      gimp_message_literal (gimp,
                            G_OBJECT (widget), GIMP_MESSAGE_WARNING,
                            "Can't merge palettes because "
                            "there are no palettes selected.");
      return;
    }

  new_palette = GIMP_PALETTE (gimp_palette_new (palette_name, FALSE));

  while (sel_list)
    {
      GimpListItem *list_item;

      list_item = GIMP_LIST_ITEM (sel_list->data);

      palette = (GimpPalette *) GIMP_VIEW (list_item->preview)->viewable;

      if (palette)
        {
          GList *cols;

          for (cols = gimp_palette_get_colors (palette);
               cols;
               cols = g_list_next (cols))
            {
              GimpPaletteEntry *entry = cols->data;

              gimp_palette_add_entry (new_palette,
                                      entry->name,
                                      &entry->color);
            }
        }

      sel_list = sel_list->next;
    }

  gimp_container_add (editor->view->container,
                      GIMP_OBJECT (new_palette));
#endif
}
static gboolean
convert_dialog_palette_filter (const GimpObject *object,
                               gpointer          user_data)
{
    GimpPalette *palette = GIMP_PALETTE (object);

    return (gimp_palette_get_n_colors (palette) > 0 &&
            gimp_palette_get_n_colors (palette) <= 256);
}
Example #8
0
static void
palette_editor_columns_changed (GtkAdjustment     *adj,
                                GimpPaletteEditor *editor)
{
  if (GIMP_DATA_EDITOR (editor)->data)
    {
      GimpPalette *palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data);

      gimp_palette_set_columns (palette,
                                ROUND (gtk_adjustment_get_value (adj)));
    }
}
Example #9
0
static void
palette_editor_color_name_changed (GtkWidget         *widget,
                                   GimpPaletteEditor *editor)
{
  if (GIMP_DATA_EDITOR (editor)->data)
    {
      GimpPalette *palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data);
      const gchar *name;

      name = gtk_entry_get_text (GTK_ENTRY (editor->color_name));

      gimp_palette_set_entry_name (palette, editor->color->position, name);
    }
}
Example #10
0
void
palette_editor_delete_color_cmd_callback (GtkAction *action,
                                          gpointer   data)
{
  GimpPaletteEditor *editor      = GIMP_PALETTE_EDITOR (data);
  GimpDataEditor    *data_editor = GIMP_DATA_EDITOR (data);

  if (data_editor->data_editable && editor->color)
    {
      GimpPalette *palette = GIMP_PALETTE (data_editor->data);

      gimp_palette_delete_entry (palette, editor->color);
    }
}
Example #11
0
void
gimp_palette_editor_pick_color (GimpPaletteEditor  *editor,
                                const GimpRGB      *color,
                                GimpColorPickState  pick_state)
{
  g_return_if_fail (GIMP_IS_PALETTE_EDITOR (editor));
  g_return_if_fail (color != NULL);

  if (GIMP_DATA_EDITOR (editor)->data_editable)
    {
      GimpPaletteEntry *entry;
      GimpData         *data;
      gint              index = -1;

      data = gimp_data_editor_get_data (GIMP_DATA_EDITOR (editor));

      switch (pick_state)
        {
        case GIMP_COLOR_PICK_STATE_NEW:
          if (editor->color)
            index = editor->color->position + 1;

          entry = gimp_palette_add_entry (GIMP_PALETTE (data), index,
                                          NULL, color);
          gimp_palette_view_select_entry (GIMP_PALETTE_VIEW (editor->view),
                                          entry);
          break;

        case GIMP_COLOR_PICK_STATE_UPDATE:
          gimp_palette_set_entry_color (GIMP_PALETTE (data),
                                        editor->color->position,
                                        color);
          break;
        }
    }
}
Example #12
0
gint
gimp_palette_editor_max_index (GimpPaletteEditor *editor)
{
  GimpPalette *palette;

  g_return_val_if_fail (GIMP_IS_PALETTE_EDITOR (editor), -1);

  palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data);

  if (palette && gimp_palette_get_n_colors (palette) > 0)
    {
      return gimp_palette_get_n_colors (palette) - 1;
    }

  return -1;
}
Example #13
0
void
palette_editor_edit_color_cmd_callback (GtkAction *action,
                                        gpointer   data)
{
  GimpPaletteEditor *editor      = GIMP_PALETTE_EDITOR (data);
  GimpDataEditor    *data_editor = GIMP_DATA_EDITOR (data);
  GimpPalette       *palette;

  if (! (data_editor->data_editable && editor->color))
    return;

  palette = GIMP_PALETTE (data_editor->data);

  if (! editor->color_dialog)
    {
      editor->color_dialog =
        gimp_color_dialog_new (GIMP_VIEWABLE (palette),
                               data_editor->context,
                               _("Edit Palette Color"),
                               GIMP_STOCK_PALETTE,
                               _("Edit Color Palette Entry"),
                               GTK_WIDGET (editor),
                               gimp_dialog_factory_get_singleton (),
                               "gimp-palette-editor-color-dialog",
                               &editor->color->color,
                               FALSE, FALSE);

      g_signal_connect (editor->color_dialog, "destroy",
                        G_CALLBACK (gtk_widget_destroyed),
                        &editor->color_dialog);

      g_signal_connect (editor->color_dialog, "update",
                        G_CALLBACK (palette_editor_edit_color_update),
                        editor);
    }
  else
    {
      gimp_viewable_dialog_set_viewable (GIMP_VIEWABLE_DIALOG (editor->color_dialog),
                                         GIMP_VIEWABLE (palette),
                                         data_editor->context);
      gimp_color_dialog_set_color (GIMP_COLOR_DIALOG (editor->color_dialog),
                                   &editor->color->color);
    }

  gtk_window_present (GTK_WINDOW (editor->color_dialog));
}
Example #14
0
GList *
gimp_palette_load_riff (const gchar  *filename,
                        GError      **error)
{
  GimpPalette *palette;
  gchar       *palette_name;
  gint         fd;
  guchar       color_bytes[4];

  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);

  fd = g_open (filename, O_RDONLY | _O_BINARY, 0);
  if (! fd)
    {
      g_set_error (error,
                   G_FILE_ERROR, g_file_error_from_errno (errno),
                   _("Could not open '%s' for reading: %s"),
                   gimp_filename_to_utf8 (filename), g_strerror (errno));
      return NULL;
    }

  palette_name = g_filename_display_basename (filename);
  palette = GIMP_PALETTE (gimp_palette_new (palette_name));
  g_free (palette_name);

  lseek (fd, 28, SEEK_SET);
  while (read (fd,
               color_bytes, sizeof (color_bytes)) == sizeof (color_bytes))
    {
      GimpRGB color;

      gimp_rgba_set_uchar (&color,
                           color_bytes[0],
                           color_bytes[1],
                           color_bytes[2],
                           255);
      gimp_palette_add_entry (palette, -1, NULL, &color);
    }

  close (fd);

  return g_list_prepend (NULL, palette);
}
Example #15
0
gboolean
gimp_palette_save (GimpData       *data,
                   GOutputStream  *output,
                   GError        **error)
{
  GimpPalette *palette = GIMP_PALETTE (data);
  GString     *string;
  GList       *list;
  gsize        bytes_written;

  string = g_string_new ("GIMP Palette\n");

  g_string_append_printf (string,
                          "Name: %s\n"
                          "Columns: %d\n"
                          "#\n",
                          gimp_object_get_name (palette),
                          CLAMP (gimp_palette_get_columns (palette), 0, 256));

  for (list = gimp_palette_get_colors (palette);
       list;
       list = g_list_next (list))
    {
      GimpPaletteEntry *entry = list->data;
      guchar            r, g, b;

      gimp_rgb_get_uchar (&entry->color, &r, &g, &b);

      g_string_append_printf (string, "%3d %3d %3d\t%s\n",
                              r, g, b, entry->name);
    }

  if (! g_output_stream_write_all (output, string->str, string->len,
                                   &bytes_written, NULL, error) ||
      bytes_written != string->len)
    {
      g_string_free (string, TRUE);

      return FALSE;
    }

  g_string_free (string, TRUE);

  return TRUE;
}
Example #16
0
static void
palette_editor_color_dropped (GimpPaletteView   *view,
                              GimpPaletteEntry  *entry,
                              const GimpRGB     *color,
                              GimpPaletteEditor *editor)
{
  if (GIMP_DATA_EDITOR (editor)->data_editable)
    {
      GimpPalette *palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data);
      gint         pos     = -1;

      if (entry)
        pos = entry->position;

      entry = gimp_palette_add_entry (palette, pos, NULL, color);
      gimp_palette_view_select_entry (GIMP_PALETTE_VIEW (editor->view), entry);
    }
}
Example #17
0
static void
palette_editor_drop_color (GtkWidget     *widget,
                           gint           x,
                           gint           y,
                           const GimpRGB *color,
                           gpointer       data)
{
  GimpPaletteEditor *editor = data;

  if (GIMP_DATA_EDITOR (editor)->data_editable)
    {
      GimpPalette      *palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data);
      GimpPaletteEntry *entry;

      entry = gimp_palette_add_entry (palette, -1, NULL, color);
      gimp_palette_view_select_entry (GIMP_PALETTE_VIEW (editor->view), entry);
    }
}
Example #18
0
void
palette_editor_new_color_cmd_callback (GtkAction *action,
                                       gint       value,
                                       gpointer   data)
{
  GimpPaletteEditor *editor      = GIMP_PALETTE_EDITOR (data);
  GimpDataEditor    *data_editor = GIMP_DATA_EDITOR (data);

  if (data_editor->data_editable)
    {
      GimpPalette *palette = GIMP_PALETTE (data_editor->data);
      GimpRGB      color;

      if (value)
        gimp_context_get_background (data_editor->context, &color);
      else
        gimp_context_get_foreground (data_editor->context, &color);

      editor->color = gimp_palette_add_entry (palette, -1, NULL, &color);
    }
}
gboolean
gimp_palette_save (GimpData  *data,
                   GError   **error)
{
  GimpPalette *palette = GIMP_PALETTE (data);
  GList       *list;
  FILE        *file;

  file = g_fopen (gimp_data_get_filename (data), "wb");

  if (! file)
    {
      g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
                   _("Could not open '%s' for writing: %s"),
                   gimp_filename_to_utf8 (gimp_data_get_filename (data)),
                   g_strerror (errno));
      return FALSE;
    }

  fprintf (file, "GIMP Palette\n");
  fprintf (file, "Name: %s\n", gimp_object_get_name (palette));
  fprintf (file, "Columns: %d\n#\n", CLAMP (gimp_palette_get_columns (palette),
                                            0, 256));

  for (list = gimp_palette_get_colors (palette);
       list;
       list = g_list_next (list))
    {
      GimpPaletteEntry *entry = list->data;
      guchar            r, g, b;

      gimp_rgb_get_uchar (&entry->color, &r, &g, &b);

      fprintf (file, "%3d %3d %3d\t%s\n", r, g, b, entry->name);
    }

  fclose (file);

  return TRUE;
}
Example #20
0
gint
gimp_palette_editor_get_index (GimpPaletteEditor *editor,
                               const GimpRGB     *search)
{
  GimpPalette *palette;

  g_return_val_if_fail (GIMP_IS_PALETTE_EDITOR (editor), -1);
  g_return_val_if_fail (search != NULL, -1);

  palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data);

  if (palette && gimp_palette_get_n_colors (palette) > 0)
    {
      GimpPaletteEntry *entry;

      entry = gimp_palette_find_entry (palette, search, editor->color);

      if (entry)
        return entry->position;
    }

  return -1;
}
Example #21
0
static void
palette_editor_resize (GimpPaletteEditor *editor,
                       gint               width,
                       gdouble            zoom_factor)
{
  GimpPalette *palette;
  gint         rows;
  gint         preview_width;
  gint         preview_height;

  palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data);

  if (! palette)
    return;

  editor->zoom_factor = zoom_factor;
  editor->last_width  = width;
  editor->col_width   = width / (editor->columns + 1) - SPACING;

  if (editor->col_width < 0)
    editor->col_width = 0;

  rows = gimp_palette_get_n_colors (palette) / editor->columns;
  if (gimp_palette_get_n_colors (palette) % editor->columns)
    rows += 1;

  preview_width  = (editor->col_width + SPACING) * editor->columns;
  preview_height = (rows *
                    (SPACING + (gint) (ENTRY_HEIGHT * editor->zoom_factor)));

  if (preview_height > GIMP_VIEWABLE_MAX_PREVIEW_SIZE)
    preview_height = ((GIMP_VIEWABLE_MAX_PREVIEW_SIZE - SPACING) / rows) * rows;

  gimp_view_renderer_set_size_full (GIMP_VIEW (editor->view)->renderer,
                                    preview_width  + SPACING,
                                    preview_height + SPACING, 0);
}
Example #22
0
static gboolean
gimp_palette_view_focus (GtkWidget        *widget,
                         GtkDirectionType  direction)
{
  GimpPaletteView *view = GIMP_PALETTE_VIEW (widget);
  GimpPalette     *palette;

  palette = GIMP_PALETTE (GIMP_VIEW (view)->renderer->viewable);

  if (gtk_widget_get_can_focus (widget) &&
      ! gtk_widget_has_focus (widget))
    {
      gtk_widget_grab_focus (widget);

      if (! view->selected &&
          palette && gimp_palette_get_n_colors (palette) > 0)
        {
          GimpPaletteEntry *entry = gimp_palette_get_entry (palette, 0);

          gimp_palette_view_select_entry (view, entry);
        }

      return TRUE;
    }

  if (view->selected)
    {
      GimpViewRendererPalette *renderer;
      gint                     skip = 0;

      renderer = GIMP_VIEW_RENDERER_PALETTE (GIMP_VIEW (view)->renderer);

      switch (direction)
        {
        case GTK_DIR_UP:
          skip = -renderer->columns;
          break;
        case GTK_DIR_DOWN:
          skip = renderer->columns;
          break;
        case GTK_DIR_LEFT:
          skip = -1;
          break;
        case GTK_DIR_RIGHT:
          skip = 1;
          break;

        case GTK_DIR_TAB_FORWARD:
        case GTK_DIR_TAB_BACKWARD:
          return FALSE;
        }

      if (skip != 0)
        {
          GimpPaletteEntry *entry;
          gint              position;

          position = view->selected->position + skip;

          entry = gimp_palette_get_entry (palette, position);

          if (entry)
            gimp_palette_view_select_entry (view, entry);
        }

      return TRUE;
    }

  return FALSE;
}
Example #23
0
GList *
gimp_palette_load_psp (GimpContext   *context,
                       GFile         *file,
                       GInputStream  *input,
                       GError       **error)
{
  GimpPalette *palette;
  gchar       *palette_name;
  guchar       color_bytes[4];
  gint         number_of_colors;
  gsize        bytes_read;
  gint         i, j;
  gboolean     color_ok;
  gchar        buffer[4096];
  /*Maximum valid file size: 256 * 4 * 3 + 256 * 2  ~= 3650 bytes */
  gchar      **lines;
  gchar      **ascii_colors;

  g_return_val_if_fail (G_IS_FILE (file), NULL);
  g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  palette_name = g_path_get_basename (gimp_file_get_utf8_name (file));
  palette = GIMP_PALETTE (gimp_palette_new (context, palette_name));
  g_free (palette_name);

  if (! g_seekable_seek (G_SEEKABLE (input), 16, G_SEEK_SET, NULL, error))
    {
      g_object_unref (palette);
      return NULL;
    }

  if (! g_input_stream_read_all (input, buffer, sizeof (buffer) - 1,
                                 &bytes_read, NULL, error))
    {
      g_object_unref (palette);
      return NULL;
    }

  buffer[bytes_read] = '\0';

  lines = g_strsplit (buffer, "\x0d\x0a", -1);

  number_of_colors = atoi (lines[0]);

  for (i = 0; i < number_of_colors; i++)
    {
      if (lines[i + 1] == NULL)
        {
          g_printerr ("Premature end of file reading %s.",
                      gimp_file_get_utf8_name (file));
          break;
        }

      ascii_colors = g_strsplit (lines[i + 1], " ", 3);
      color_ok = TRUE;

      for (j = 0 ; j < 3; j++)
        {
          if (ascii_colors[j] == NULL)
            {
              g_printerr ("Corrupted palette file %s.",
                          gimp_file_get_utf8_name (file));
              color_ok = FALSE;
              break;
            }

          color_bytes[j] = atoi (ascii_colors[j]);
        }

      if (color_ok)
        {
          GimpRGB color;

          gimp_rgba_set_uchar (&color,
                               color_bytes[0],
                               color_bytes[1],
                               color_bytes[2],
                               255);
          gimp_palette_add_entry (palette, -1, NULL, &color);
        }

      g_strfreev (ascii_colors);
    }

  g_strfreev (lines);

  return g_list_prepend (NULL, palette);
}
Example #24
0
GList *
gimp_palette_load_aco (const gchar  *filename,
                       GError      **error)
{
  GimpPalette *palette;
  gchar       *palette_name;
  gint         fd;
  gint         format_version;
  gint         number_of_colors;
  gint         i;
  gchar        header[4];
  gchar        color_info[10];
  gchar        format2_preamble[4];
  gint         status;

  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);

  fd = g_open (filename, O_RDONLY | _O_BINARY, 0);
  if (! fd)
    {
      g_set_error (error,
                   G_FILE_ERROR, g_file_error_from_errno (errno),
                   _("Could not open '%s' for reading: %s"),
                   gimp_filename_to_utf8 (filename), g_strerror (errno));
      return NULL;
    }

  palette_name = g_filename_display_basename (filename);
  palette = GIMP_PALETTE (gimp_palette_new (palette_name));
  g_free (palette_name);

  status = read(fd, header, sizeof (header));

  if (status < 0)
    {
      close(fd);

      return g_list_prepend (NULL, palette);
    }

  format_version = header[1] + (header[0] << 8);
  number_of_colors = header[3] + (header[2] << 8);

  for (i = 0; i < number_of_colors; i++)
  {
      gint     color_space;
      gint     w, x, y, z;
      gboolean color_ok = FALSE;
      GimpRGB  color;

      read (fd, color_info, sizeof (color_info));

      color_space = color_info[1] + (color_info[0] << 8);

      w = (guchar) color_info[3] + ((guchar) color_info[2] << 8);
      x = (guchar) color_info[5] + ((guchar) color_info[4] << 8);
      y = (guchar) color_info[7] + ((guchar) color_info[6] << 8);
      z = (guchar) color_info[9] + ((guchar) color_info[8] << 8);

      if (color_space == 0) /* RGB */
	{
	  gdouble R = ((gdouble) w) / 65536.0;
	  gdouble G = ((gdouble) x) / 65536.0;
	  gdouble B = ((gdouble) y) / 65536.0;

	  gimp_rgba_set (&color, R, G, B, 1.0);

	  color_ok = TRUE;
	}
      else if (color_space == 1) /* HSV */
	{
	  GimpHSV hsv;

	  gdouble H = ((gdouble) w) / 65536.0;
	  gdouble S = ((gdouble) x) / 65536.0;
	  gdouble V = ((gdouble) y) / 65536.0;

	  gimp_hsva_set (&hsv, H, S, V, 1.0);
	  gimp_hsv_to_rgb (&hsv, &color);

	  color_ok = TRUE;
	}
      else if (color_space == 2) /* CMYK */
	{
	  GimpCMYK cmyk;

	  gdouble C = 1.0 - (((gdouble) w) / 65536.0);
	  gdouble M = 1.0 - (((gdouble) x) / 65536.0);
	  gdouble Y = 1.0 - (((gdouble) y) / 65536.0);
	  gdouble K = 1.0 - (((gdouble) z) / 65536.0);

	  gimp_cmyka_set (&cmyk, C, M, Y, K, 1.0);
	  gimp_cmyk_to_rgb (&cmyk, &color);

	  color_ok = TRUE;
	}
      else if (color_space == 8) /* Grayscale */
	{
	  gdouble K = 1.0 - (((gdouble) w) / 10000.0);

	  gimp_rgba_set (&color, K, K, K, 1.0);

	  color_ok = TRUE;
	}
      else if (color_space == 9) /* Wide? CMYK */
	{
	  GimpCMYK cmyk;

	  gdouble C = 1.0 - (((gdouble) w) / 10000.0);
	  gdouble M = 1.0 - (((gdouble) x) / 10000.0);
	  gdouble Y = 1.0 - (((gdouble) y) / 10000.0);
	  gdouble K = 1.0 - (((gdouble) z) / 10000.0);

	  gimp_cmyka_set (&cmyk, C, M, Y, K, 1.0);
	  gimp_cmyk_to_rgb (&cmyk, &color);

	  color_ok = TRUE;
	}
      else
	{
	  g_printerr ("Unsupported color space (%d) in ACO file %s\n",
                      color_space, gimp_filename_to_utf8 (filename));
	}

      if (format_version == 2)
	{
	  gint number_of_chars;

	  read (fd, format2_preamble, sizeof (format2_preamble));
	  number_of_chars = format2_preamble[3] + (format2_preamble[2] << 8);
	  lseek (fd, number_of_chars * 2, SEEK_SET);
	}

      if (color_ok)
        gimp_palette_add_entry (palette, -1, NULL, &color);
  }

  close(fd);

  return g_list_prepend (NULL, palette);
}
Example #25
0
GList *
gimp_palette_load_css (const gchar  *filename,
		       GError      **error)
{
  GimpPalette *palette;
  gchar       *name;
  FILE        *file;
  GRegex      *regex;
  GimpRGB      color;

  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);

  regex = g_regex_new (".*color.*:(?P<param>.*);", G_REGEX_CASELESS, 0, error);
  if (! regex)
    return 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;
    }

  name = g_filename_display_basename (filename);
  palette = GIMP_PALETTE (gimp_palette_new (name));
  g_free (name);

  do
    {
      GMatchInfo *matches;
      gchar       buf[1024];

      if (fgets (buf, sizeof (buf), file) != NULL)
	{
	  if (g_regex_match (regex, buf, 0, &matches))
	    {
	      gchar *word = g_match_info_fetch_named (matches, "param");

	      if (gimp_rgb_parse_css (&color, word, -1))
		{
		  if (! gimp_palette_find_entry (palette, &color, NULL))
		    {
		      gimp_palette_add_entry (palette, -1, NULL, &color);
		    }
		}

	      g_free (word);
	    }
	}
    } while (! feof (file));

  fclose (file);

  g_regex_unref (regex);

  return g_list_prepend (NULL, palette);
}
Example #26
0
GList *
gimp_palette_load_psp (const gchar  *filename,
                       GError      **error)
{
  GimpPalette *palette;
  gchar       *palette_name;
  gint         fd;
  guchar       color_bytes[4];
  gint         number_of_colors;
  gint         data_size;
  gint         i, j;
  gboolean     color_ok;
  gchar        buffer[4096];
  /*Maximum valid file size: 256 * 4 * 3 + 256 * 2  ~= 3650 bytes */
  gchar      **lines;
  gchar      **ascii_colors;

  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);

  fd = g_open (filename, O_RDONLY | _O_BINARY, 0);
  if (! fd)
    {
      g_set_error (error,
                   G_FILE_ERROR, g_file_error_from_errno (errno),
                   _("Could not open '%s' for reading: %s"),
                   gimp_filename_to_utf8 (filename), g_strerror (errno));
      return NULL;
    }

  palette_name = g_filename_display_basename (filename);
  palette = GIMP_PALETTE (gimp_palette_new (palette_name));
  g_free (palette_name);

  lseek (fd, 16, SEEK_SET);
  data_size = read (fd, buffer, sizeof (buffer) - 1);
  buffer[data_size] = '\0';

  lines = g_strsplit (buffer, "\x0d\x0a", -1);

  number_of_colors = atoi (lines[0]);

  for (i = 0; i < number_of_colors; i++)
    {
      if (lines[i + 1] == NULL)
        {
          g_printerr ("Premature end of file reading %s.",
                      gimp_filename_to_utf8 (filename));
          break;
        }

      ascii_colors = g_strsplit (lines[i + 1], " ", 3);
      color_ok = TRUE;

      for (j = 0 ; j < 3; j++)
        {
          if (ascii_colors[j] == NULL)
            {
              g_printerr ("Corrupted palette file %s.",
                          gimp_filename_to_utf8 (filename));
              color_ok = FALSE;
              break;
            }

          color_bytes[j] = atoi (ascii_colors[j]);
        }

      if (color_ok)
        {
          GimpRGB color;

          gimp_rgba_set_uchar (&color,
                               color_bytes[0],
                               color_bytes[1],
                               color_bytes[2],
                               255);
          gimp_palette_add_entry (palette, -1, NULL, &color);
        }

      g_strfreev (ascii_colors);
    }

  g_strfreev (lines);

  close (fd);

  return g_list_prepend (NULL, palette);
}
Example #27
0
void
gimp_palette_editor_zoom (GimpPaletteEditor  *editor,
                          GimpZoomType        zoom_type)
{
  GimpPalette *palette;
  gdouble      zoom_factor;

  g_return_if_fail (GIMP_IS_PALETTE_EDITOR (editor));

  palette = GIMP_PALETTE (GIMP_DATA_EDITOR (editor)->data);

  if (! palette)
    return;

  zoom_factor = editor->zoom_factor;

  switch (zoom_type)
    {
    case GIMP_ZOOM_IN_MAX:
    case GIMP_ZOOM_IN_MORE:
    case GIMP_ZOOM_IN:
      zoom_factor += 0.1;
      break;

    case GIMP_ZOOM_OUT_MORE:
    case GIMP_ZOOM_OUT:
      zoom_factor -= 0.1;
      break;

    case GIMP_ZOOM_OUT_MAX:
    case GIMP_ZOOM_TO: /* abused as ZOOM_ALL */
      {
        GtkWidget     *scrolled_win = GIMP_DATA_EDITOR (editor)->view;
        GtkWidget     *viewport     = gtk_bin_get_child (GTK_BIN (scrolled_win));
        GtkAllocation  allocation;
        gint           columns;
        gint           rows;

        gtk_widget_get_allocation (viewport, &allocation);

        columns = gimp_palette_get_columns (palette);
        if (columns == 0)
          columns = COLUMNS;

        rows = gimp_palette_get_n_colors (palette) / columns;
        if (gimp_palette_get_n_colors (palette) % columns)
          rows += 1;

        rows = MAX (1, rows);

        zoom_factor = (((gdouble) allocation.height - 2 * SPACING) /
                       (gdouble) rows - SPACING) / ENTRY_HEIGHT;
      }
      break;
    }

  zoom_factor = CLAMP (zoom_factor, 0.1, 4.0);

  editor->columns = gimp_palette_get_columns (palette);
  if (editor->columns == 0)
    editor->columns = COLUMNS;

  palette_editor_resize (editor, editor->last_width, zoom_factor);

  palette_editor_scroll_top_left (editor);
}
static void
gimp_view_renderer_palette_render (GimpViewRenderer *renderer,
                                   GtkWidget        *widget)
{
  GimpViewRendererPalette *renderpal = GIMP_VIEW_RENDERER_PALETTE (renderer);
  GimpPalette             *palette;
  guchar                  *row;
  guchar                  *dest;
  GList                   *list;
  gdouble                  cell_width;
  gint                     grid_width;
  gint                     dest_stride;
  gint                     y;

  palette = GIMP_PALETTE (renderer->viewable);

  if (palette->n_colors < 1)
    return;

  grid_width = renderpal->draw_grid ? 1 : 0;

  if (renderpal->cell_size > 0)
    {
      if (palette->n_columns > 0)
        cell_width = MAX ((gdouble) renderpal->cell_size,
                          (gdouble) (renderer->width - grid_width) /
                          (gdouble) palette->n_columns);
      else
        cell_width = renderpal->cell_size;
    }
  else
    {
      if (palette->n_columns > 0)
        cell_width = ((gdouble) (renderer->width - grid_width) /
                      (gdouble) palette->n_columns);
      else
        cell_width = (gdouble) (renderer->width - grid_width) / 16.0;
    }

  cell_width = MAX (4.0, cell_width);

  renderpal->cell_width = cell_width;

  renderpal->columns = (gdouble) (renderer->width - grid_width) / cell_width;

  renderpal->rows = palette->n_colors / renderpal->columns;
  if (palette->n_colors % renderpal->columns)
    renderpal->rows += 1;

  renderpal->cell_height = MAX (4, ((renderer->height - grid_width) /
                                    renderpal->rows));

  if (! renderpal->draw_grid)
    renderpal->cell_height = MIN (renderpal->cell_height,
                                  renderpal->cell_width);

  list = palette->colors;

  if (! renderer->surface)
    renderer->surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
                                                    renderer->width,
                                                    renderer->height);

  cairo_surface_flush (renderer->surface);

  row = g_new (guchar, renderer->width * 4);

  dest        = cairo_image_surface_get_data (renderer->surface);
  dest_stride = cairo_image_surface_get_stride (renderer->surface);

  for (y = 0; y < renderer->height; y++)
    {
      if ((y % renderpal->cell_height) == 0)
        {
          guchar  r, g, b;
          gint    x;
          gint    n = 0;
          guchar *d = row;

          memset (row, renderpal->draw_grid ? 0 : 255, renderer->width * 4);

          r = g = b = (renderpal->draw_grid ? 0 : 255);

          for (x = 0; x < renderer->width; x++, d += 4)
            {
              if ((x % renderpal->cell_width) == 0)
                {
                  if (list && n < renderpal->columns &&
                      renderer->width - x >= renderpal->cell_width)
                    {
                      GimpPaletteEntry *entry = list->data;

                      list = g_list_next (list);
                      n++;

                      gimp_rgb_get_uchar (&entry->color, &r, &g, &b);
                    }
                  else
                    {
                      r = g = b = (renderpal->draw_grid ? 0 : 255);
                    }
                }

              if (renderpal->draw_grid && (x % renderpal->cell_width) == 0)
                {
                  GIMP_CAIRO_RGB24_SET_PIXEL (d, 0, 0, 0);
                }
              else
                {
                  GIMP_CAIRO_RGB24_SET_PIXEL (d, r, g, b);
                }
            }
        }

      if (renderpal->draw_grid && (y % renderpal->cell_height) == 0)
        {
          memset (dest, 0, renderer->width * 4);
        }
      else
        {
          memcpy (dest, row, renderer->width * 4);
        }

      dest += dest_stride;
    }

  g_free (row);

  cairo_surface_mark_dirty (renderer->surface);

  renderer->needs_render = FALSE;
}
Example #29
0
GList *
gimp_palette_load_css (GimpContext   *context,
                       GFile         *file,
                       GInputStream  *input,
                       GError       **error)
{
  GimpPalette      *palette;
  GDataInputStream *data_input;
  gchar            *name;
  GRegex           *regex;
  gchar            *buf;

  g_return_val_if_fail (G_IS_FILE (file), NULL);
  g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  regex = g_regex_new (".*color.*:(?P<param>.*);", G_REGEX_CASELESS, 0, error);
  if (! regex)
    return NULL;

  name = g_path_get_basename (gimp_file_get_utf8_name (file));
  palette = GIMP_PALETTE (gimp_palette_new (context, name));
  g_free (name);

  data_input = g_data_input_stream_new (input);

  do
    {
      gsize  buf_len = 1024;

      buf = g_data_input_stream_read_line (data_input, &buf_len, NULL, NULL);

      if (buf)
        {
          GMatchInfo *matches;

          if (g_regex_match (regex, buf, 0, &matches))
            {
              GimpRGB  color;
              gchar   *word = g_match_info_fetch_named (matches, "param");

              if (gimp_rgb_parse_css (&color, word, -1))
                {
                  if (! gimp_palette_find_entry (palette, &color, NULL))
                    {
                      gimp_palette_add_entry (palette, -1, NULL, &color);
                    }
                }

              g_free (word);
            }
          g_match_info_free (matches);
          g_free (buf);
        }
    }
  while (buf);

  g_regex_unref (regex);
  g_object_unref (data_input);

  return g_list_prepend (NULL, palette);
}
Example #30
0
GList *
gimp_palette_load_aco (GimpContext   *context,
                       GFile         *file,
                       GInputStream  *input,
                       GError       **error)
{
  GimpPalette *palette;
  gchar       *palette_name;
  gint         format_version;
  gint         number_of_colors;
  gint         i;
  gchar        header[4];
  gsize        bytes_read;

  g_return_val_if_fail (G_IS_FILE (file), NULL);
  g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  if (! g_input_stream_read_all (input, header, sizeof (header),
                                 &bytes_read, NULL, error) ||
      bytes_read != sizeof (header))
    {
      g_prefix_error (error,
                      _("Could not read header from palette file '%s': "),
                      gimp_file_get_utf8_name (file));
      return NULL;
    }

  palette_name = g_path_get_basename (gimp_file_get_utf8_name (file));
  palette = GIMP_PALETTE (gimp_palette_new (context, palette_name));
  g_free (palette_name);

  format_version   = header[1] + (header[0] << 8);
  number_of_colors = header[3] + (header[2] << 8);

  for (i = 0; i < number_of_colors; i++)
    {
      gchar     color_info[10];
      gint      color_space;
      gint      w, x, y, z;
      gboolean  color_ok = FALSE;
      GimpRGB   color;
      GError   *my_error = NULL;

      if (! g_input_stream_read_all (input, color_info, sizeof (color_info),
                                     &bytes_read, NULL, &my_error) ||
          bytes_read != sizeof (color_info))
        {
          if (palette->colors)
            {
              g_message (_("Reading palette file '%s': "
                           "Read %d colors from truncated file: %s"),
                         gimp_file_get_utf8_name (file),
                         g_list_length (palette->colors),
                         my_error ?
                         my_error->message : _("Premature end of file."));
              g_clear_error (&my_error);
              break;
            }

          g_propagate_error (error, my_error);
          g_object_unref (palette);

          return NULL;
        }

      color_space = color_info[1] + (color_info[0] << 8);

      w = (guchar) color_info[3] + ((guchar) color_info[2] << 8);
      x = (guchar) color_info[5] + ((guchar) color_info[4] << 8);
      y = (guchar) color_info[7] + ((guchar) color_info[6] << 8);
      z = (guchar) color_info[9] + ((guchar) color_info[8] << 8);

      if (color_space == 0) /* RGB */
        {
          gdouble R = ((gdouble) w) / 65536.0;
          gdouble G = ((gdouble) x) / 65536.0;
          gdouble B = ((gdouble) y) / 65536.0;

          gimp_rgba_set (&color, R, G, B, 1.0);

          color_ok = TRUE;
        }
      else if (color_space == 1) /* HSV */
        {
          GimpHSV hsv;

          gdouble H = ((gdouble) w) / 65536.0;
          gdouble S = ((gdouble) x) / 65536.0;
          gdouble V = ((gdouble) y) / 65536.0;

          gimp_hsva_set (&hsv, H, S, V, 1.0);
          gimp_hsv_to_rgb (&hsv, &color);

          color_ok = TRUE;
        }
      else if (color_space == 2) /* CMYK */
        {
          GimpCMYK cmyk;

          gdouble C = 1.0 - (((gdouble) w) / 65536.0);
          gdouble M = 1.0 - (((gdouble) x) / 65536.0);
          gdouble Y = 1.0 - (((gdouble) y) / 65536.0);
          gdouble K = 1.0 - (((gdouble) z) / 65536.0);

          gimp_cmyka_set (&cmyk, C, M, Y, K, 1.0);
          gimp_cmyk_to_rgb (&cmyk, &color);

          color_ok = TRUE;
        }
      else if (color_space == 8) /* Grayscale */
        {
          gdouble K = 1.0 - (((gdouble) w) / 10000.0);

          gimp_rgba_set (&color, K, K, K, 1.0);

          color_ok = TRUE;
        }
      else if (color_space == 9) /* Wide? CMYK */
        {
          GimpCMYK cmyk;

          gdouble C = 1.0 - (((gdouble) w) / 10000.0);
          gdouble M = 1.0 - (((gdouble) x) / 10000.0);
          gdouble Y = 1.0 - (((gdouble) y) / 10000.0);
          gdouble K = 1.0 - (((gdouble) z) / 10000.0);

          gimp_cmyka_set (&cmyk, C, M, Y, K, 1.0);
          gimp_cmyk_to_rgb (&cmyk, &color);

          color_ok = TRUE;
        }
      else
        {
          g_printerr ("Unsupported color space (%d) in ACO file %s\n",
                      color_space, gimp_file_get_utf8_name (file));
        }

      if (format_version == 2)
        {
          gchar format2_preamble[4];
          gint  number_of_chars;

          if (! g_input_stream_read_all (input,
                                         format2_preamble,
                                         sizeof (format2_preamble),
                                         &bytes_read, NULL, error) ||
              bytes_read != sizeof (format2_preamble))
            {
              g_object_unref (palette);
              return NULL;
            }

          number_of_chars = format2_preamble[3] + (format2_preamble[2] << 8);

          if (! g_seekable_seek (G_SEEKABLE (input), number_of_chars * 2,
                                 G_SEEK_SET, NULL, error))
            {
              g_object_unref (palette);
              return NULL;
            }
        }

      if (color_ok)
        gimp_palette_add_entry (palette, -1, NULL, &color);
    }

  return g_list_prepend (NULL, palette);
}