2001-01-22 03:53:56 +08:00
|
|
|
/* The GIMP -- an image manipulation program
|
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (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
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2001-11-21 05:24:33 +08:00
|
|
|
#include <errno.h>
|
2005-02-07 09:24:22 +08:00
|
|
|
|
2001-01-22 03:53:56 +08:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2001-08-14 22:53:55 +08:00
|
|
|
#include <glib-object.h>
|
2005-02-07 09:24:22 +08:00
|
|
|
#include <glib/gstdio.h>
|
2001-01-22 03:53:56 +08:00
|
|
|
|
2003-10-16 20:24:58 +08:00
|
|
|
#include "libgimpbase/gimpbase.h"
|
2001-01-24 02:49:44 +08:00
|
|
|
#include "libgimpcolor/gimpcolor.h"
|
|
|
|
|
2001-05-10 06:34:59 +08:00
|
|
|
#include "core-types.h"
|
2001-01-22 03:53:56 +08:00
|
|
|
|
2001-05-15 19:25:25 +08:00
|
|
|
#include "base/temp-buf.h"
|
|
|
|
|
2001-01-22 03:53:56 +08:00
|
|
|
#include "gimppalette.h"
|
2001-05-10 06:34:59 +08:00
|
|
|
|
2003-03-26 00:38:19 +08:00
|
|
|
#include "gimp-intl.h"
|
2001-01-22 03:53:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* local function prototypes */
|
|
|
|
|
2001-08-11 22:39:19 +08:00
|
|
|
static void gimp_palette_finalize (GObject *object);
|
|
|
|
|
2003-11-17 01:51:36 +08:00
|
|
|
static gint64 gimp_palette_get_memsize (GimpObject *object,
|
|
|
|
gint64 *gui_size);
|
2002-01-31 00:14:26 +08:00
|
|
|
|
2003-02-22 03:03:19 +08:00
|
|
|
static void gimp_palette_get_preview_size (GimpViewable *viewable,
|
|
|
|
gint size,
|
|
|
|
gboolean popup,
|
|
|
|
gboolean dot_for_dot,
|
|
|
|
gint *width,
|
|
|
|
gint *height);
|
2003-02-27 21:59:41 +08:00
|
|
|
static gboolean gimp_palette_get_popup_size (GimpViewable *viewable,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
|
|
|
gboolean dot_for_dot,
|
|
|
|
gint *popup_width,
|
|
|
|
gint *popup_height);
|
2001-02-12 00:14:25 +08:00
|
|
|
static TempBuf * gimp_palette_get_new_preview (GimpViewable *viewable,
|
|
|
|
gint width,
|
|
|
|
gint height);
|
2003-04-09 00:01:01 +08:00
|
|
|
static gchar * gimp_palette_get_description (GimpViewable *viewable,
|
|
|
|
gchar **tooltip);
|
2002-12-02 21:39:09 +08:00
|
|
|
static gboolean gimp_palette_save (GimpData *data,
|
|
|
|
GError **error);
|
2001-02-14 03:53:07 +08:00
|
|
|
static gchar * gimp_palette_get_extension (GimpData *data);
|
2005-06-08 19:27:31 +08:00
|
|
|
static GimpData * gimp_palette_duplicate (GimpData *data);
|
2001-01-22 03:53:56 +08:00
|
|
|
|
2001-02-12 00:14:25 +08:00
|
|
|
static void gimp_palette_entry_free (GimpPaletteEntry *entry);
|
2001-01-22 03:53:56 +08:00
|
|
|
|
|
|
|
|
2006-05-15 17:46:31 +08:00
|
|
|
G_DEFINE_TYPE (GimpPalette, gimp_palette, GIMP_TYPE_DATA)
|
2001-01-22 03:53:56 +08:00
|
|
|
|
2005-12-11 03:24:36 +08:00
|
|
|
#define parent_class gimp_palette_parent_class
|
2001-01-22 03:53:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_palette_class_init (GimpPaletteClass *klass)
|
|
|
|
{
|
2004-06-30 19:09:44 +08:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
GimpObjectClass *gimp_object_class = GIMP_OBJECT_CLASS (klass);
|
|
|
|
GimpViewableClass *viewable_class = GIMP_VIEWABLE_CLASS (klass);
|
|
|
|
GimpDataClass *data_class = GIMP_DATA_CLASS (klass);
|
2001-01-22 03:53:56 +08:00
|
|
|
|
2003-02-22 03:03:19 +08:00
|
|
|
object_class->finalize = gimp_palette_finalize;
|
2001-01-22 03:53:56 +08:00
|
|
|
|
2003-02-22 03:03:19 +08:00
|
|
|
gimp_object_class->get_memsize = gimp_palette_get_memsize;
|
2002-01-31 00:14:26 +08:00
|
|
|
|
2003-11-17 21:34:38 +08:00
|
|
|
viewable_class->default_stock_id = "gtk-select-color";
|
2003-02-22 03:03:19 +08:00
|
|
|
viewable_class->get_preview_size = gimp_palette_get_preview_size;
|
2003-02-27 21:59:41 +08:00
|
|
|
viewable_class->get_popup_size = gimp_palette_get_popup_size;
|
2003-02-22 03:03:19 +08:00
|
|
|
viewable_class->get_new_preview = gimp_palette_get_new_preview;
|
2003-04-09 00:01:01 +08:00
|
|
|
viewable_class->get_description = gimp_palette_get_description;
|
2001-02-12 00:14:25 +08:00
|
|
|
|
2003-02-22 03:03:19 +08:00
|
|
|
data_class->save = gimp_palette_save;
|
|
|
|
data_class->get_extension = gimp_palette_get_extension;
|
2003-03-11 01:35:42 +08:00
|
|
|
data_class->duplicate = gimp_palette_duplicate;
|
2001-01-22 03:53:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_palette_init (GimpPalette *palette)
|
|
|
|
{
|
|
|
|
palette->colors = NULL;
|
|
|
|
palette->n_colors = 0;
|
2001-02-13 02:05:12 +08:00
|
|
|
palette->n_columns = 0;
|
2001-01-22 03:53:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-08-11 22:39:19 +08:00
|
|
|
gimp_palette_finalize (GObject *object)
|
2001-01-22 03:53:56 +08:00
|
|
|
{
|
2003-11-17 01:51:36 +08:00
|
|
|
GimpPalette *palette = GIMP_PALETTE (object);
|
2001-01-22 03:53:56 +08:00
|
|
|
|
Port to glib/gtk+ 2.0 episode I (every segfault has it's beginning)
2001-07-24 Michael Natterer <mitch@gimp.org>
Port to glib/gtk+ 2.0 episode I (every segfault has it's beginning)
* configure.in: require glib/gtk+ >= 1.3.7, commented out the
gtkxmhtml stuff.
From now on, you will need glib, pango, atk and gtk+ HEAD from CVS
to hack or use GIMP HEAD.
Beware, it crashes randomly :)
* app/core/Makefile.am
* app/core/gimpmarshal.list: new file plus rules to generate
gimpmarshal.[ch] from it.
* app/core/*
* app/tools/*
* app/widgets/*
* libgimpwidgets/*: started to use the glib object system. All
core/ objects are still gtk objects however. All signals are
created using g_signal_new(). There are many gtk+ artefacts left.
Finally, we will _not_ use the gtk_signal_foo() wrappers and
friends any more.
* app/colormaps.c
* app/devices.[ch]
* app/disp_callbacks.c
* app/errorconsole.c
* app/file-save.[ch]
* app/interface.c
* app/module_db.c
* app/nav_window.c
* app/ops_buttons.c
* app/scroll.c
* app/user_install.c
* app/gui/about-dialog.c
* app/gui/brush-editor.c
* app/gui/brushes-commands.c
* app/gui/color-notebook.c
* app/gui/colormap-dialog.c
* app/gui/dialogs-commands.c
* app/gui/dialogs-constructors.c
* app/gui/file-commands.c
* app/gui/file-dialog-utils.c
* app/gui/file-new-dialog.c
* app/gui/file-open-dialog.[ch]
* app/gui/file-save-dialog.c
* app/gui/gradient-editor.c
* app/gui/gradients-commands.c
* app/gui/image-commands.c
* app/gui/info-dialog.[ch]
* app/gui/layer-select.c
* app/gui/layers-commands.c
* app/gui/menus.c
* app/gui/offset-dialog.c
* app/gui/palette-editor.c
* app/gui/palettes-commands.c
* app/gui/patterns-commands.c
* app/gui/preferences-dialog.c
* app/gui/resize-dialog.[ch]
* app/gui/splash.c
* app/gui/tips-dialog.c
* app/gui/tool-options-dialog.c
* app/gui/toolbox.c
* app/gui/tools-commands.c
* libgimp/gimpbrushmenu.c
* libgimp/gimpmenu.c
* libgimp/gimppatternmenu.c
* libgimp/gimpui.c
* libgimpbase/gimpenv.c: tons and tons of changes like "const
gchar*", switch from GdkDeviceInfo to GdkDevice (very incomplete
and currently disables), lots of s/gtk_signal/g_signal/,
removal/replacement of deprecated stuff,
s/GtkSignalFunc/GCallback/ and lots of small changes and fixes
while I was on it, zillions of warnings left...
* modules/Makefile.am: disabled the water color selector
temporarily (XInput issues).
* plug-ins/Makefile.am
* plug-ins/common/.cvsignore
* plug-ins/common/Makefile.am
* plug-ins/common/plugin-defs.pl: simply excluded all plug-ins
which did not build (including Script-Fu). They are trivial to
fix.
2001-07-25 05:27:11 +08:00
|
|
|
if (palette->colors)
|
2001-01-22 03:53:56 +08:00
|
|
|
{
|
Port to glib/gtk+ 2.0 episode I (every segfault has it's beginning)
2001-07-24 Michael Natterer <mitch@gimp.org>
Port to glib/gtk+ 2.0 episode I (every segfault has it's beginning)
* configure.in: require glib/gtk+ >= 1.3.7, commented out the
gtkxmhtml stuff.
From now on, you will need glib, pango, atk and gtk+ HEAD from CVS
to hack or use GIMP HEAD.
Beware, it crashes randomly :)
* app/core/Makefile.am
* app/core/gimpmarshal.list: new file plus rules to generate
gimpmarshal.[ch] from it.
* app/core/*
* app/tools/*
* app/widgets/*
* libgimpwidgets/*: started to use the glib object system. All
core/ objects are still gtk objects however. All signals are
created using g_signal_new(). There are many gtk+ artefacts left.
Finally, we will _not_ use the gtk_signal_foo() wrappers and
friends any more.
* app/colormaps.c
* app/devices.[ch]
* app/disp_callbacks.c
* app/errorconsole.c
* app/file-save.[ch]
* app/interface.c
* app/module_db.c
* app/nav_window.c
* app/ops_buttons.c
* app/scroll.c
* app/user_install.c
* app/gui/about-dialog.c
* app/gui/brush-editor.c
* app/gui/brushes-commands.c
* app/gui/color-notebook.c
* app/gui/colormap-dialog.c
* app/gui/dialogs-commands.c
* app/gui/dialogs-constructors.c
* app/gui/file-commands.c
* app/gui/file-dialog-utils.c
* app/gui/file-new-dialog.c
* app/gui/file-open-dialog.[ch]
* app/gui/file-save-dialog.c
* app/gui/gradient-editor.c
* app/gui/gradients-commands.c
* app/gui/image-commands.c
* app/gui/info-dialog.[ch]
* app/gui/layer-select.c
* app/gui/layers-commands.c
* app/gui/menus.c
* app/gui/offset-dialog.c
* app/gui/palette-editor.c
* app/gui/palettes-commands.c
* app/gui/patterns-commands.c
* app/gui/preferences-dialog.c
* app/gui/resize-dialog.[ch]
* app/gui/splash.c
* app/gui/tips-dialog.c
* app/gui/tool-options-dialog.c
* app/gui/toolbox.c
* app/gui/tools-commands.c
* libgimp/gimpbrushmenu.c
* libgimp/gimpmenu.c
* libgimp/gimppatternmenu.c
* libgimp/gimpui.c
* libgimpbase/gimpenv.c: tons and tons of changes like "const
gchar*", switch from GdkDeviceInfo to GdkDevice (very incomplete
and currently disables), lots of s/gtk_signal/g_signal/,
removal/replacement of deprecated stuff,
s/GtkSignalFunc/GCallback/ and lots of small changes and fixes
while I was on it, zillions of warnings left...
* modules/Makefile.am: disabled the water color selector
temporarily (XInput issues).
* plug-ins/Makefile.am
* plug-ins/common/.cvsignore
* plug-ins/common/Makefile.am
* plug-ins/common/plugin-defs.pl: simply excluded all plug-ins
which did not build (including Script-Fu). They are trivial to
fix.
2001-07-25 05:27:11 +08:00
|
|
|
g_list_foreach (palette->colors, (GFunc) gimp_palette_entry_free, NULL);
|
|
|
|
g_list_free (palette->colors);
|
|
|
|
palette->colors = NULL;
|
2001-01-22 03:53:56 +08:00
|
|
|
}
|
|
|
|
|
2001-08-11 22:39:19 +08:00
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
2001-01-22 03:53:56 +08:00
|
|
|
}
|
|
|
|
|
2003-11-17 01:51:36 +08:00
|
|
|
static gint64
|
2003-08-25 18:49:33 +08:00
|
|
|
gimp_palette_get_memsize (GimpObject *object,
|
2003-11-17 01:51:36 +08:00
|
|
|
gint64 *gui_size)
|
2002-01-31 00:14:26 +08:00
|
|
|
{
|
2004-01-30 00:19:57 +08:00
|
|
|
GimpPalette *palette = GIMP_PALETTE (object);
|
2002-01-31 00:14:26 +08:00
|
|
|
GList *list;
|
2003-11-17 01:51:36 +08:00
|
|
|
gint64 memsize = 0;
|
2002-01-31 00:14:26 +08:00
|
|
|
|
|
|
|
for (list = palette->colors; list; list = g_list_next (list))
|
|
|
|
{
|
2004-01-30 00:19:57 +08:00
|
|
|
GimpPaletteEntry *entry = list->data;
|
2002-01-31 00:14:26 +08:00
|
|
|
|
|
|
|
memsize += sizeof (GList) + sizeof (GimpPaletteEntry);
|
|
|
|
|
|
|
|
if (entry->name)
|
|
|
|
memsize += strlen (entry->name) + 1;
|
|
|
|
}
|
|
|
|
|
2003-08-25 18:49:33 +08:00
|
|
|
return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
|
|
|
|
gui_size);
|
2002-01-31 00:14:26 +08:00
|
|
|
}
|
|
|
|
|
2003-02-22 03:03:19 +08:00
|
|
|
static void
|
|
|
|
gimp_palette_get_preview_size (GimpViewable *viewable,
|
|
|
|
gint size,
|
|
|
|
gboolean popup,
|
|
|
|
gboolean dot_for_dot,
|
|
|
|
gint *width,
|
|
|
|
gint *height)
|
|
|
|
{
|
|
|
|
*width = size;
|
|
|
|
*height = size / 2;
|
|
|
|
}
|
|
|
|
|
2003-02-27 21:59:41 +08:00
|
|
|
static gboolean
|
|
|
|
gimp_palette_get_popup_size (GimpViewable *viewable,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
|
|
|
gboolean dot_for_dot,
|
|
|
|
gint *popup_width,
|
|
|
|
gint *popup_height)
|
|
|
|
{
|
2004-01-30 00:19:57 +08:00
|
|
|
GimpPalette *palette = GIMP_PALETTE (viewable);
|
2003-02-27 21:59:41 +08:00
|
|
|
gint p_width;
|
|
|
|
gint p_height;
|
|
|
|
|
|
|
|
if (! palette->n_colors)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (palette->n_columns)
|
|
|
|
p_width = palette->n_columns;
|
|
|
|
else
|
|
|
|
p_width = MIN (palette->n_colors, 16);
|
|
|
|
|
|
|
|
p_height = MAX (1, palette->n_colors / p_width);
|
|
|
|
|
|
|
|
if (p_width * 4 > width || p_height * 4 > height)
|
|
|
|
{
|
|
|
|
*popup_width = p_width * 4;
|
|
|
|
*popup_height = p_height * 4;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2001-02-12 00:14:25 +08:00
|
|
|
static TempBuf *
|
|
|
|
gimp_palette_get_new_preview (GimpViewable *viewable,
|
2004-06-30 19:09:44 +08:00
|
|
|
gint width,
|
|
|
|
gint height)
|
2001-02-12 00:14:25 +08:00
|
|
|
{
|
2004-09-28 22:59:55 +08:00
|
|
|
GimpPalette *palette = GIMP_PALETTE (viewable);
|
|
|
|
TempBuf *temp_buf;
|
|
|
|
guchar *buf;
|
|
|
|
guchar *b;
|
|
|
|
GList *list;
|
|
|
|
guchar white[3] = { 255, 255, 255 };
|
|
|
|
gint columns;
|
|
|
|
gint rows;
|
|
|
|
gint cell_size;
|
|
|
|
gint x, y;
|
|
|
|
|
|
|
|
temp_buf = temp_buf_new (width, height, 3, 0, 0, white);
|
2001-02-12 00:14:25 +08:00
|
|
|
|
2002-10-09 22:04:25 +08:00
|
|
|
if (palette->n_columns > 1)
|
|
|
|
cell_size = MAX (4, width / palette->n_columns);
|
|
|
|
else
|
|
|
|
cell_size = 4;
|
2001-02-13 02:05:12 +08:00
|
|
|
|
2002-10-09 22:04:25 +08:00
|
|
|
columns = width / cell_size;
|
|
|
|
rows = height / cell_size;
|
2001-02-12 00:14:25 +08:00
|
|
|
|
|
|
|
buf = temp_buf_data (temp_buf);
|
|
|
|
b = g_new (guchar, width * 3);
|
|
|
|
|
|
|
|
list = palette->colors;
|
|
|
|
|
|
|
|
for (y = 0; y < rows && list; y++)
|
|
|
|
{
|
2004-09-28 22:59:55 +08:00
|
|
|
gint i;
|
|
|
|
|
2002-10-09 22:04:25 +08:00
|
|
|
memset (b, 255, width * 3);
|
|
|
|
|
2001-02-12 00:14:25 +08:00
|
|
|
for (x = 0; x < columns && list; x++)
|
2004-06-30 19:09:44 +08:00
|
|
|
{
|
2004-09-28 22:59:55 +08:00
|
|
|
GimpPaletteEntry *entry = list->data;
|
2001-02-12 00:14:25 +08:00
|
|
|
|
2004-06-30 19:09:44 +08:00
|
|
|
list = g_list_next (list);
|
2001-02-12 00:14:25 +08:00
|
|
|
|
2004-06-30 19:09:44 +08:00
|
|
|
gimp_rgb_get_uchar (&entry->color,
|
|
|
|
&b[x * cell_size * 3 + 0],
|
|
|
|
&b[x * cell_size * 3 + 1],
|
|
|
|
&b[x * cell_size * 3 + 2]);
|
2001-02-12 00:14:25 +08:00
|
|
|
|
2004-06-30 19:09:44 +08:00
|
|
|
for (i = 1; i < cell_size; i++)
|
|
|
|
{
|
|
|
|
b[(x * cell_size + i) * 3 + 0] = b[(x * cell_size) * 3 + 0];
|
|
|
|
b[(x * cell_size + i) * 3 + 1] = b[(x * cell_size) * 3 + 1];
|
|
|
|
b[(x * cell_size + i) * 3 + 2] = b[(x * cell_size) * 3 + 2];
|
|
|
|
}
|
|
|
|
}
|
2001-02-12 00:14:25 +08:00
|
|
|
|
2002-10-09 22:04:25 +08:00
|
|
|
for (i = 0; i < cell_size; i++)
|
2004-09-28 22:59:55 +08:00
|
|
|
memcpy (buf + ((y * cell_size + i) * width) * 3, b, width * 3);
|
2001-02-12 00:14:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free (b);
|
|
|
|
|
|
|
|
return temp_buf;
|
|
|
|
}
|
|
|
|
|
2003-04-09 00:01:01 +08:00
|
|
|
static gchar *
|
|
|
|
gimp_palette_get_description (GimpViewable *viewable,
|
|
|
|
gchar **tooltip)
|
|
|
|
{
|
2003-11-17 01:51:36 +08:00
|
|
|
GimpPalette *palette = GIMP_PALETTE (viewable);
|
2003-04-09 00:01:01 +08:00
|
|
|
|
|
|
|
return g_strdup_printf ("%s (%d)",
|
|
|
|
GIMP_OBJECT (palette)->name,
|
|
|
|
palette->n_colors);
|
|
|
|
}
|
|
|
|
|
2001-02-14 22:57:14 +08:00
|
|
|
GimpData *
|
2005-06-08 19:27:31 +08:00
|
|
|
gimp_palette_new (const gchar *name)
|
2001-01-22 03:53:56 +08:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (name != NULL, NULL);
|
|
|
|
g_return_val_if_fail (*name != '\0', NULL);
|
|
|
|
|
2004-01-30 00:19:57 +08:00
|
|
|
return g_object_new (GIMP_TYPE_PALETTE,
|
|
|
|
"name", name,
|
|
|
|
NULL);
|
2001-01-22 03:53:56 +08:00
|
|
|
}
|
|
|
|
|
2001-02-14 22:57:14 +08:00
|
|
|
GimpData *
|
2001-02-14 03:53:07 +08:00
|
|
|
gimp_palette_get_standard (void)
|
|
|
|
{
|
2004-01-30 00:19:57 +08:00
|
|
|
static GimpData *standard_palette = NULL;
|
2001-02-14 03:53:07 +08:00
|
|
|
|
|
|
|
if (! standard_palette)
|
|
|
|
{
|
2005-06-08 19:27:31 +08:00
|
|
|
standard_palette = gimp_palette_new ("Standard");
|
2004-01-30 00:19:57 +08:00
|
|
|
|
2004-02-13 19:53:22 +08:00
|
|
|
standard_palette->dirty = FALSE;
|
|
|
|
gimp_data_make_internal (standard_palette);
|
2001-02-14 03:53:07 +08:00
|
|
|
|
2004-01-30 00:19:57 +08:00
|
|
|
g_object_ref (standard_palette);
|
2001-02-14 03:53:07 +08:00
|
|
|
}
|
|
|
|
|
2004-01-30 00:19:57 +08:00
|
|
|
return standard_palette;
|
2001-02-14 03:53:07 +08:00
|
|
|
}
|
|
|
|
|
2004-07-27 03:00:22 +08:00
|
|
|
GList *
|
2002-12-02 21:39:09 +08:00
|
|
|
gimp_palette_load (const gchar *filename,
|
|
|
|
GError **error)
|
2001-01-22 03:53:56 +08:00
|
|
|
{
|
2004-06-13 06:28:13 +08:00
|
|
|
GimpPalette *palette;
|
|
|
|
GimpPaletteEntry *entry;
|
|
|
|
gchar str[1024];
|
|
|
|
gchar *tok;
|
|
|
|
FILE *file;
|
|
|
|
gint r, g, b;
|
|
|
|
gint linenum;
|
2001-01-22 03:53:56 +08:00
|
|
|
|
|
|
|
g_return_val_if_fail (filename != NULL, NULL);
|
2004-01-30 00:19:57 +08:00
|
|
|
g_return_val_if_fail (g_path_is_absolute (filename), NULL);
|
2002-12-02 21:39:09 +08:00
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
2001-01-22 03:53:56 +08:00
|
|
|
|
|
|
|
r = g = b = 0;
|
|
|
|
|
2005-02-07 09:24:22 +08:00
|
|
|
file = g_fopen (filename, "r");
|
2004-01-30 00:19:57 +08:00
|
|
|
|
|
|
|
if (! file)
|
2001-01-22 03:53:56 +08:00
|
|
|
{
|
2002-12-02 21:39:09 +08:00
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
|
|
|
|
_("Could not open '%s' for reading: %s"),
|
2004-01-19 09:08:43 +08:00
|
|
|
gimp_filename_to_utf8 (filename), g_strerror (errno));
|
2001-01-22 03:53:56 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
linenum = 0;
|
|
|
|
|
2004-01-30 00:19:57 +08:00
|
|
|
fread (str, 13, 1, file);
|
2001-01-22 03:53:56 +08:00
|
|
|
str[13] = '\0';
|
|
|
|
linenum++;
|
|
|
|
if (strcmp (str, "GIMP Palette\n"))
|
|
|
|
{
|
|
|
|
/* bad magic, but maybe it has \r\n at the end of lines? */
|
|
|
|
if (!strcmp (str, "GIMP Palette\r"))
|
2004-06-30 19:09:44 +08:00
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
2003-11-14 23:33:40 +08:00
|
|
|
_("Fatal parse error in palette file '%s': "
|
|
|
|
"Missing magic header.\n"
|
2002-12-02 21:39:09 +08:00
|
|
|
"Does this file need converting from DOS?"),
|
2004-01-19 09:08:43 +08:00
|
|
|
gimp_filename_to_utf8 (filename));
|
2001-01-22 03:53:56 +08:00
|
|
|
else
|
2004-06-30 19:09:44 +08:00
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
2003-11-14 23:33:40 +08:00
|
|
|
_("Fatal parse error in palette file '%s': "
|
|
|
|
"Missing magic header."),
|
2004-01-19 09:08:43 +08:00
|
|
|
gimp_filename_to_utf8 (filename));
|
2001-01-22 03:53:56 +08:00
|
|
|
|
2004-01-30 00:19:57 +08:00
|
|
|
fclose (file);
|
2001-01-22 03:53:56 +08:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-05-26 07:25:45 +08:00
|
|
|
palette = g_object_new (GIMP_TYPE_PALETTE,
|
|
|
|
"mime-type", "application/x-gimp-palette",
|
|
|
|
NULL);
|
2001-02-12 00:14:25 +08:00
|
|
|
|
2004-06-13 06:28:13 +08:00
|
|
|
if (! fgets (str, sizeof (str), file))
|
2001-01-22 03:53:56 +08:00
|
|
|
{
|
2002-12-02 21:39:09 +08:00
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
2003-11-14 23:33:40 +08:00
|
|
|
_("Fatal parse error in palette file '%s': "
|
|
|
|
"Read error in line %d."),
|
2004-01-19 09:08:43 +08:00
|
|
|
gimp_filename_to_utf8 (filename), linenum);
|
2004-01-30 00:19:57 +08:00
|
|
|
fclose (file);
|
2003-01-06 06:07:10 +08:00
|
|
|
g_object_unref (palette);
|
2001-02-12 00:14:25 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
linenum++;
|
|
|
|
|
|
|
|
if (! strncmp (str, "Name: ", strlen ("Name: ")))
|
|
|
|
{
|
2003-10-16 20:24:58 +08:00
|
|
|
gchar *utf8;
|
|
|
|
|
|
|
|
utf8 = gimp_any_to_utf8 (&str[strlen ("Name: ")], -1,
|
|
|
|
_("Invalid UTF-8 string in palette file '%s'"),
|
2004-01-19 09:08:43 +08:00
|
|
|
gimp_filename_to_utf8 (filename));
|
2003-10-16 20:24:58 +08:00
|
|
|
|
2006-04-07 18:51:22 +08:00
|
|
|
gimp_object_take_name (GIMP_OBJECT (palette), g_strstrip (utf8));
|
2001-02-12 00:14:25 +08:00
|
|
|
|
2004-06-13 06:28:13 +08:00
|
|
|
if (! fgets (str, sizeof (str), file))
|
2004-06-30 19:09:44 +08:00
|
|
|
{
|
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
2003-11-14 23:33:40 +08:00
|
|
|
_("Fatal parse error in palette file '%s': "
|
|
|
|
"Read error in line %d."),
|
2004-01-19 09:08:43 +08:00
|
|
|
gimp_filename_to_utf8 (filename), linenum);
|
2004-06-30 19:09:44 +08:00
|
|
|
fclose (file);
|
|
|
|
g_object_unref (palette);
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-01-22 03:53:56 +08:00
|
|
|
|
|
|
|
linenum++;
|
2001-02-13 02:05:12 +08:00
|
|
|
|
|
|
|
if (! strncmp (str, "Columns: ", strlen ("Columns: ")))
|
2004-06-30 19:09:44 +08:00
|
|
|
{
|
|
|
|
gint columns;
|
2001-02-13 02:05:12 +08:00
|
|
|
|
2004-06-30 19:09:44 +08:00
|
|
|
columns = atoi (g_strstrip (&str[strlen ("Columns: ")]));
|
2001-02-13 02:05:12 +08:00
|
|
|
|
2004-06-30 19:09:44 +08:00
|
|
|
if (columns < 0 || columns > 256)
|
|
|
|
{
|
|
|
|
g_message (_("Reading palette file '%s': "
|
|
|
|
"Invalid number of columns in line %d. "
|
2003-11-14 23:33:40 +08:00
|
|
|
"Using default value."),
|
2004-06-30 19:09:44 +08:00
|
|
|
gimp_filename_to_utf8 (filename), linenum);
|
|
|
|
columns = 0;
|
|
|
|
}
|
2001-02-13 02:05:12 +08:00
|
|
|
|
2004-06-30 19:09:44 +08:00
|
|
|
palette->n_columns = columns;
|
2001-02-13 02:05:12 +08:00
|
|
|
|
2004-06-30 19:09:44 +08:00
|
|
|
if (! fgets (str, sizeof (str), file))
|
|
|
|
{
|
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
2003-11-14 23:33:40 +08:00
|
|
|
_("Fatal parse error in palette file '%s': "
|
|
|
|
"Read error in line %d."),
|
2004-01-19 09:08:43 +08:00
|
|
|
gimp_filename_to_utf8 (filename), linenum);
|
2004-06-30 19:09:44 +08:00
|
|
|
fclose (file);
|
|
|
|
g_object_unref (palette);
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-02-13 02:05:12 +08:00
|
|
|
|
2004-06-30 19:09:44 +08:00
|
|
|
linenum++;
|
|
|
|
}
|
2001-02-12 00:14:25 +08:00
|
|
|
}
|
|
|
|
else /* old palette format */
|
|
|
|
{
|
2006-04-07 18:51:22 +08:00
|
|
|
gimp_object_take_name (GIMP_OBJECT (palette),
|
|
|
|
g_filename_display_basename (filename));
|
2001-02-12 00:14:25 +08:00
|
|
|
}
|
|
|
|
|
2004-01-30 00:19:57 +08:00
|
|
|
while (! feof (file))
|
2001-02-12 00:14:25 +08:00
|
|
|
{
|
2001-01-22 03:53:56 +08:00
|
|
|
if (str[0] != '#')
|
2004-06-30 19:09:44 +08:00
|
|
|
{
|
|
|
|
tok = strtok (str, " \t");
|
|
|
|
if (tok)
|
|
|
|
r = atoi (tok);
|
|
|
|
else
|
|
|
|
/* maybe we should just abort? */
|
|
|
|
g_message (_("Reading palette file '%s': "
|
|
|
|
"Missing RED component in line %d."),
|
2004-01-19 09:08:43 +08:00
|
|
|
gimp_filename_to_utf8 (filename), linenum);
|
2001-01-22 03:53:56 +08:00
|
|
|
|
2004-06-30 19:09:44 +08:00
|
|
|
tok = strtok (NULL, " \t");
|
|
|
|
if (tok)
|
|
|
|
g = atoi (tok);
|
|
|
|
else
|
|
|
|
g_message (_("Reading palette '%s': "
|
|
|
|
"Missing GREEN component in line %d."),
|
2004-01-19 09:08:43 +08:00
|
|
|
gimp_filename_to_utf8 (filename), linenum);
|
2001-01-22 03:53:56 +08:00
|
|
|
|
2004-06-30 19:09:44 +08:00
|
|
|
tok = strtok (NULL, " \t");
|
|
|
|
if (tok)
|
|
|
|
b = atoi (tok);
|
|
|
|
else
|
|
|
|
g_message (_("Reading palette file '%s': "
|
|
|
|
"Missing BLUE component in line %d."),
|
2004-01-19 09:08:43 +08:00
|
|
|
gimp_filename_to_utf8 (filename), linenum);
|
2001-01-22 03:53:56 +08:00
|
|
|
|
2004-06-30 19:09:44 +08:00
|
|
|
/* optional name */
|
|
|
|
tok = strtok (NULL, "\n");
|
2001-01-22 03:53:56 +08:00
|
|
|
|
2004-06-30 19:09:44 +08:00
|
|
|
if (r < 0 || r > 255 ||
|
|
|
|
g < 0 || g > 255 ||
|
|
|
|
b < 0 || b > 255)
|
|
|
|
g_message (_("Reading palette file '%s': "
|
|
|
|
"RGB value out of range in line %d."),
|
2004-01-19 09:08:43 +08:00
|
|
|
gimp_filename_to_utf8 (filename), linenum);
|
2001-01-22 03:53:56 +08:00
|
|
|
|
2004-06-13 06:28:13 +08:00
|
|
|
/* don't call gimp_palette_add_entry here, it's rather inefficient */
|
|
|
|
entry = g_new0 (GimpPaletteEntry, 1);
|
|
|
|
|
2004-06-30 19:09:44 +08:00
|
|
|
gimp_rgba_set_uchar (&entry->color,
|
|
|
|
(guchar) r,
|
|
|
|
(guchar) g,
|
|
|
|
(guchar) b,
|
|
|
|
255);
|
2001-01-22 03:53:56 +08:00
|
|
|
|
2004-06-13 06:28:13 +08:00
|
|
|
entry->name = g_strdup (tok ? tok : _("Untitled"));
|
|
|
|
entry->position = palette->n_colors;
|
|
|
|
|
|
|
|
palette->colors = g_list_prepend (palette->colors, entry);
|
|
|
|
palette->n_colors++;
|
2004-06-30 19:09:44 +08:00
|
|
|
}
|
2001-02-12 00:14:25 +08:00
|
|
|
|
2004-06-13 06:28:13 +08:00
|
|
|
if (! fgets (str, sizeof (str), file))
|
2004-06-30 19:09:44 +08:00
|
|
|
{
|
|
|
|
if (feof (file))
|
|
|
|
break;
|
2001-02-12 00:14:25 +08:00
|
|
|
|
2004-06-30 19:09:44 +08:00
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_READ,
|
2003-11-14 23:33:40 +08:00
|
|
|
_("Fatal parse error in palette file '%s': "
|
|
|
|
"Read error in line %d."),
|
2004-01-19 09:08:43 +08:00
|
|
|
gimp_filename_to_utf8 (filename), linenum);
|
2004-06-30 19:09:44 +08:00
|
|
|
fclose (file);
|
|
|
|
g_object_unref (palette);
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-02-12 00:14:25 +08:00
|
|
|
|
|
|
|
linenum++;
|
2001-01-22 03:53:56 +08:00
|
|
|
}
|
|
|
|
|
2004-01-30 00:19:57 +08:00
|
|
|
fclose (file);
|
2001-01-22 03:53:56 +08:00
|
|
|
|
2004-06-13 06:28:13 +08:00
|
|
|
palette->colors = g_list_reverse (palette->colors);
|
|
|
|
|
2004-07-27 03:00:22 +08:00
|
|
|
return g_list_prepend (NULL, palette);
|
2001-01-22 03:53:56 +08:00
|
|
|
}
|
|
|
|
|
2001-02-12 00:14:25 +08:00
|
|
|
static gboolean
|
2002-12-02 21:39:09 +08:00
|
|
|
gimp_palette_save (GimpData *data,
|
|
|
|
GError **error)
|
2001-02-12 00:14:25 +08:00
|
|
|
{
|
2004-01-30 00:19:57 +08:00
|
|
|
GimpPalette *palette = GIMP_PALETTE (data);
|
|
|
|
GList *list;
|
|
|
|
FILE *file;
|
2001-01-22 03:53:56 +08:00
|
|
|
|
2005-02-07 09:24:22 +08:00
|
|
|
file = g_fopen (data->filename, "w");
|
2001-01-22 03:53:56 +08:00
|
|
|
|
2004-01-30 00:19:57 +08:00
|
|
|
if (! file)
|
2001-01-22 03:53:56 +08:00
|
|
|
{
|
2002-12-02 21:39:09 +08:00
|
|
|
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
|
|
|
|
_("Could not open '%s' for writing: %s"),
|
2004-01-19 09:08:43 +08:00
|
|
|
gimp_filename_to_utf8 (data->filename),
|
2004-06-30 19:09:44 +08:00
|
|
|
g_strerror (errno));
|
2001-01-22 03:53:56 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-01-30 00:19:57 +08:00
|
|
|
fprintf (file, "GIMP Palette\n");
|
|
|
|
fprintf (file, "Name: %s\n", GIMP_OBJECT (palette)->name);
|
|
|
|
fprintf (file, "Columns: %d\n#\n", CLAMP (palette->n_columns, 0, 256));
|
2001-01-22 03:53:56 +08:00
|
|
|
|
|
|
|
for (list = palette->colors; list; list = g_list_next (list))
|
|
|
|
{
|
2004-01-30 00:19:57 +08:00
|
|
|
GimpPaletteEntry *entry = list->data;
|
|
|
|
guchar r, g, b;
|
2001-01-22 03:53:56 +08:00
|
|
|
|
|
|
|
gimp_rgb_get_uchar (&entry->color, &r, &g, &b);
|
|
|
|
|
2004-01-30 00:19:57 +08:00
|
|
|
fprintf (file, "%3d %3d %3d\t%s\n", r, g, b, entry->name);
|
2001-01-22 03:53:56 +08:00
|
|
|
}
|
|
|
|
|
2004-01-30 00:19:57 +08:00
|
|
|
fclose (file);
|
2001-01-22 03:53:56 +08:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2001-02-14 03:53:07 +08:00
|
|
|
static gchar *
|
|
|
|
gimp_palette_get_extension (GimpData *data)
|
|
|
|
{
|
|
|
|
return GIMP_PALETTE_FILE_EXTENSION;
|
|
|
|
}
|
|
|
|
|
2003-03-11 01:35:42 +08:00
|
|
|
static GimpData *
|
2005-06-08 19:27:31 +08:00
|
|
|
gimp_palette_duplicate (GimpData *data)
|
2003-03-11 01:35:42 +08:00
|
|
|
{
|
2004-01-30 00:19:57 +08:00
|
|
|
GimpPalette *palette = GIMP_PALETTE (data);
|
2003-03-11 01:35:42 +08:00
|
|
|
GimpPalette *new;
|
|
|
|
GList *list;
|
|
|
|
|
|
|
|
new = g_object_new (GIMP_TYPE_PALETTE, NULL);
|
|
|
|
|
|
|
|
new->n_columns = palette->n_columns;
|
|
|
|
|
|
|
|
for (list = palette->colors; list; list = g_list_next (list))
|
|
|
|
{
|
|
|
|
GimpPaletteEntry *entry = list->data;
|
|
|
|
|
2005-01-01 01:27:57 +08:00
|
|
|
gimp_palette_add_entry (new, -1, entry->name, &entry->color);
|
2003-03-11 01:35:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return GIMP_DATA (new);
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:53:56 +08:00
|
|
|
GimpPaletteEntry *
|
2005-07-14 01:46:12 +08:00
|
|
|
gimp_palette_add_entry (GimpPalette *palette,
|
|
|
|
gint position,
|
|
|
|
const gchar *name,
|
|
|
|
const GimpRGB *color)
|
2001-01-22 03:53:56 +08:00
|
|
|
{
|
|
|
|
GimpPaletteEntry *entry;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GIMP_IS_PALETTE (palette), NULL);
|
|
|
|
g_return_val_if_fail (color != NULL, NULL);
|
|
|
|
|
|
|
|
entry = g_new0 (GimpPaletteEntry, 1);
|
|
|
|
|
2005-07-14 01:46:12 +08:00
|
|
|
entry->color = *color;
|
|
|
|
entry->name = g_strdup (name ? name : _("Untitled"));
|
2004-12-31 22:36:30 +08:00
|
|
|
|
|
|
|
if (position < 0 || position >= palette->n_colors)
|
2005-01-01 01:27:57 +08:00
|
|
|
{
|
|
|
|
entry->position = palette->n_colors;
|
|
|
|
palette->colors = g_list_append (palette->colors, entry);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GList *list;
|
2004-12-31 22:36:30 +08:00
|
|
|
|
2005-01-01 01:27:57 +08:00
|
|
|
entry->position = position;
|
|
|
|
palette->colors = g_list_insert (palette->colors, entry, position);
|
2004-12-31 22:36:30 +08:00
|
|
|
|
2005-01-01 01:27:57 +08:00
|
|
|
/* renumber the displaced entries */
|
|
|
|
for (list = g_list_nth (palette->colors, position + 1);
|
|
|
|
list;
|
|
|
|
list = g_list_next (list))
|
|
|
|
{
|
2005-07-14 01:46:12 +08:00
|
|
|
GimpPaletteEntry *entry2 = list->data;
|
|
|
|
|
|
|
|
entry2->position += 1;
|
2005-01-01 01:27:57 +08:00
|
|
|
}
|
|
|
|
}
|
2004-12-31 22:36:30 +08:00
|
|
|
|
|
|
|
palette->n_colors += 1;
|
|
|
|
|
2006-05-23 23:39:45 +08:00
|
|
|
gimp_data_dirty (GIMP_DATA (palette));
|
2004-12-31 22:36:30 +08:00
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2001-01-22 03:53:56 +08:00
|
|
|
void
|
|
|
|
gimp_palette_delete_entry (GimpPalette *palette,
|
2004-06-30 19:09:44 +08:00
|
|
|
GimpPaletteEntry *entry)
|
2001-01-22 03:53:56 +08:00
|
|
|
{
|
|
|
|
GList *list;
|
|
|
|
gint pos = 0;
|
|
|
|
|
|
|
|
g_return_if_fail (GIMP_IS_PALETTE (palette));
|
|
|
|
g_return_if_fail (entry != NULL);
|
|
|
|
|
|
|
|
if (g_list_find (palette->colors, entry))
|
|
|
|
{
|
|
|
|
pos = entry->position;
|
|
|
|
gimp_palette_entry_free (entry);
|
|
|
|
|
|
|
|
palette->colors = g_list_remove (palette->colors, entry);
|
|
|
|
|
|
|
|
palette->n_colors--;
|
|
|
|
|
|
|
|
for (list = g_list_nth (palette->colors, pos);
|
2004-06-30 19:09:44 +08:00
|
|
|
list;
|
|
|
|
list = g_list_next (list))
|
|
|
|
{
|
|
|
|
entry = (GimpPaletteEntry *) list->data;
|
2001-01-22 03:53:56 +08:00
|
|
|
|
2004-06-30 19:09:44 +08:00
|
|
|
entry->position = pos++;
|
|
|
|
}
|
2001-01-22 03:53:56 +08:00
|
|
|
|
2006-05-23 23:39:45 +08:00
|
|
|
gimp_data_dirty (GIMP_DATA (palette));
|
2001-01-22 03:53:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-09 00:34:42 +08:00
|
|
|
void
|
2005-03-09 08:50:09 +08:00
|
|
|
gimp_palette_set_columns (GimpPalette *palette,
|
|
|
|
gint columns)
|
2002-03-09 00:34:42 +08:00
|
|
|
{
|
|
|
|
g_return_if_fail (GIMP_IS_PALETTE (palette));
|
|
|
|
|
2005-03-08 22:44:29 +08:00
|
|
|
columns = CLAMP (columns, 0, 64);
|
2002-03-09 00:34:42 +08:00
|
|
|
|
2005-03-08 22:44:29 +08:00
|
|
|
if (palette->n_columns != columns)
|
2002-03-09 00:34:42 +08:00
|
|
|
{
|
2005-03-08 22:44:29 +08:00
|
|
|
palette->n_columns = columns;
|
2002-03-09 00:34:42 +08:00
|
|
|
|
|
|
|
gimp_data_dirty (GIMP_DATA (palette));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gint
|
2005-03-09 08:50:09 +08:00
|
|
|
gimp_palette_get_columns (GimpPalette *palette)
|
2002-03-09 00:34:42 +08:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (GIMP_IS_PALETTE (palette), 0);
|
|
|
|
|
|
|
|
return palette->n_columns;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* private functions */
|
|
|
|
|
2001-01-22 03:53:56 +08:00
|
|
|
static void
|
|
|
|
gimp_palette_entry_free (GimpPaletteEntry *entry)
|
|
|
|
{
|
|
|
|
g_return_if_fail (entry != NULL);
|
|
|
|
|
|
|
|
g_free (entry->name);
|
|
|
|
g_free (entry);
|
|
|
|
}
|