2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
1997-11-25 06:05:25 +08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
1997-11-25 06:05:25 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
1997-11-25 06:05:25 +08:00
|
|
|
* (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
|
2018-07-12 05:27:07 +08:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
1997-11-25 06:05:25 +08:00
|
|
|
*/
|
|
|
|
|
2020-02-02 01:00:07 +08:00
|
|
|
/* XPM plugin version 1.2.7 */
|
1998-03-17 08:31:58 +08:00
|
|
|
|
2000-04-14 16:59:52 +08:00
|
|
|
/*
|
2020-02-02 01:00:07 +08:00
|
|
|
1.2.7 fixes saving unused transparency (bug #4560)
|
|
|
|
|
2003-03-31 17:34:02 +08:00
|
|
|
1.2.6 fixes crash when saving indexed images (bug #109567)
|
|
|
|
|
2003-03-11 05:07:05 +08:00
|
|
|
1.2.5 only creates a "None" color entry if the image has alpha (bug #108034)
|
|
|
|
|
2002-07-10 00:17:43 +08:00
|
|
|
1.2.4 displays an error message if saving fails (bug #87588)
|
|
|
|
|
2000-04-14 16:59:52 +08:00
|
|
|
1.2.3 fixes bug when running in noninteractive mode
|
|
|
|
changes alpha_threshold range from [0, 1] to [0,255] for consistency with
|
|
|
|
the threshold_alpha plugin
|
|
|
|
|
|
|
|
1.2.2 fixes bug that generated bad digits on images with more than 20000
|
|
|
|
colors. (thanks, yanele)
|
1998-03-17 08:31:58 +08:00
|
|
|
parses gtkrc (thanks, yosh)
|
|
|
|
doesn't load parameter screen on images that don't have alpha
|
|
|
|
|
|
|
|
1.2.1 fixes some minor bugs -- spaces in #XXXXXX strings, small typos in code.
|
|
|
|
|
|
|
|
1.2 compute color indexes so that we don't have to use XpmSaveXImage*
|
|
|
|
|
|
|
|
Previous...Inherited code from Ray Lehtiniemi, who inherited it from S & P.
|
|
|
|
*/
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1999-05-30 00:35:47 +08:00
|
|
|
#include "config.h"
|
2000-01-11 23:48:00 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
#include <string.h>
|
2000-01-11 23:48:00 +08:00
|
|
|
|
2005-03-04 23:12:29 +08:00
|
|
|
#include <glib/gstdio.h>
|
2000-01-11 23:48:00 +08:00
|
|
|
|
2010-12-29 22:19:50 +08:00
|
|
|
#include <gdk/gdk.h> /* For GDK_WINDOWING_WIN32 */
|
2005-05-13 20:50:35 +08:00
|
|
|
|
2006-12-06 19:01:37 +08:00
|
|
|
#ifndef GDK_WINDOWING_X11
|
2003-07-27 17:46:38 +08:00
|
|
|
#ifndef XPM_NO_X
|
|
|
|
#define XPM_NO_X
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <X11/xpm.h>
|
|
|
|
|
2000-01-11 23:48:00 +08:00
|
|
|
#include <libgimp/gimp.h>
|
|
|
|
#include <libgimp/gimpui.h>
|
|
|
|
|
1999-05-30 00:35:47 +08:00
|
|
|
#include "libgimp/stdplugins-intl.h"
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-05-02 04:22:55 +08:00
|
|
|
|
2005-08-16 06:42:34 +08:00
|
|
|
#define LOAD_PROC "file-xpm-load"
|
|
|
|
#define SAVE_PROC "file-xpm-save"
|
2008-08-11 18:06:13 +08:00
|
|
|
#define PLUG_IN_BINARY "file-xpm"
|
2011-04-09 02:31:34 +08:00
|
|
|
#define PLUG_IN_ROLE "gimp-file-xpm"
|
2005-08-16 06:42:34 +08:00
|
|
|
#define SCALE_WIDTH 125
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
|
1998-02-25 14:21:26 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
1999-10-04 07:48:33 +08:00
|
|
|
guchar r;
|
|
|
|
guchar g;
|
|
|
|
guchar b;
|
1998-02-25 14:21:26 +08:00
|
|
|
} rgbkey;
|
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
|
|
|
|
typedef struct _Xpm Xpm;
|
|
|
|
typedef struct _XpmClass XpmClass;
|
|
|
|
|
|
|
|
struct _Xpm
|
|
|
|
{
|
|
|
|
GimpPlugIn parent_instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _XpmClass
|
|
|
|
{
|
|
|
|
GimpPlugInClass parent_class;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#define XPM_TYPE (xpm_get_type ())
|
|
|
|
#define XPM (obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), XPM_TYPE, Xpm))
|
|
|
|
|
|
|
|
GType xpm_get_type (void) G_GNUC_CONST;
|
|
|
|
|
|
|
|
static GList * xpm_query_procedures (GimpPlugIn *plug_in);
|
|
|
|
static GimpProcedure * xpm_create_procedure (GimpPlugIn *plug_in,
|
|
|
|
const gchar *name);
|
|
|
|
|
|
|
|
static GimpValueArray * xpm_load (GimpProcedure *procedure,
|
|
|
|
GimpRunMode run_mode,
|
|
|
|
GFile *file,
|
|
|
|
const GimpValueArray *args,
|
|
|
|
gpointer run_data);
|
|
|
|
static GimpValueArray * xpm_save (GimpProcedure *procedure,
|
|
|
|
GimpRunMode run_mode,
|
|
|
|
GimpImage *image,
|
2020-04-14 17:46:17 +08:00
|
|
|
gint n_drawables,
|
|
|
|
GimpDrawable **drawables,
|
2019-08-25 00:56:03 +08:00
|
|
|
GFile *file,
|
|
|
|
const GimpValueArray *args,
|
|
|
|
gpointer run_data);
|
|
|
|
|
2019-09-12 03:48:34 +08:00
|
|
|
static GimpImage * load_image (GFile *file,
|
2019-08-25 00:56:03 +08:00
|
|
|
GError **error);
|
|
|
|
static guchar * parse_colors (XpmImage *xpm_image);
|
|
|
|
static void parse_image (GimpImage *image,
|
|
|
|
XpmImage *xpm_image,
|
|
|
|
guchar *cmap);
|
2019-09-12 03:48:34 +08:00
|
|
|
static gboolean save_image (GFile *file,
|
2019-08-25 00:56:03 +08:00
|
|
|
GimpImage *image,
|
|
|
|
GimpDrawable *drawable,
|
2019-09-26 18:09:41 +08:00
|
|
|
GObject *config,
|
2019-08-25 00:56:03 +08:00
|
|
|
GError **error);
|
2019-09-26 18:09:41 +08:00
|
|
|
static gboolean save_dialog (GimpProcedure *procedure,
|
|
|
|
GObject *config);
|
2019-08-25 00:56:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (Xpm, xpm, GIMP_TYPE_PLUG_IN)
|
|
|
|
|
|
|
|
GIMP_MAIN (XPM_TYPE)
|
|
|
|
|
|
|
|
|
|
|
|
static const gchar linenoise [] =
|
|
|
|
" .+@#$%&*=-;>,')!~{]^/(_:<[}|1234567890abcdefghijklmnopqrstuvwxyz\
|
|
|
|
ABCDEFGHIJKLMNOPQRSTUVWXYZ`";
|
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
/* whether the image is color or not. global so I only have to pass
|
|
|
|
* one user value to the GHFunc
|
|
|
|
*/
|
2002-06-30 05:01:33 +08:00
|
|
|
static gboolean color;
|
2000-01-26 01:46:56 +08:00
|
|
|
|
|
|
|
/* bytes per pixel. global so I only have to pass one user value
|
|
|
|
* to the GHFunc
|
|
|
|
*/
|
2002-06-30 05:01:33 +08:00
|
|
|
static gint cpp;
|
1998-02-25 14:21:26 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
static void
|
2019-08-25 00:56:03 +08:00
|
|
|
xpm_class_init (XpmClass *klass)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2019-08-25 00:56:03 +08:00
|
|
|
GimpPlugInClass *plug_in_class = GIMP_PLUG_IN_CLASS (klass);
|
|
|
|
|
|
|
|
plug_in_class->query_procedures = xpm_query_procedures;
|
|
|
|
plug_in_class->create_procedure = xpm_create_procedure;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-08-25 00:56:03 +08:00
|
|
|
xpm_init (Xpm *xpm)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2019-08-25 00:56:03 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
static GList *
|
|
|
|
xpm_query_procedures (GimpPlugIn *plug_in)
|
|
|
|
{
|
|
|
|
GList *list = NULL;
|
2012-11-26 09:39:44 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
list = g_list_append (list, g_strdup (LOAD_PROC));
|
|
|
|
list = g_list_append (list, g_strdup (SAVE_PROC));
|
2003-03-26 00:38:19 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
return list;
|
|
|
|
}
|
2002-01-16 02:35:29 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
static GimpProcedure *
|
|
|
|
xpm_create_procedure (GimpPlugIn *plug_in,
|
|
|
|
const gchar *name)
|
|
|
|
{
|
|
|
|
GimpProcedure *procedure = NULL;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
if (! strcmp (name, LOAD_PROC))
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2019-08-30 18:52:28 +08:00
|
|
|
procedure = gimp_load_procedure_new (plug_in, name,
|
|
|
|
GIMP_PDB_PROC_TYPE_PLUGIN,
|
2019-08-25 00:56:03 +08:00
|
|
|
xpm_load, NULL, NULL);
|
|
|
|
|
|
|
|
gimp_procedure_set_menu_label (procedure, N_("X PixMap image"));
|
|
|
|
|
|
|
|
gimp_procedure_set_documentation (procedure,
|
|
|
|
"Load files in XPM (X11 Pixmap) format.",
|
|
|
|
"Load files in XPM (X11 Pixmap) format. "
|
|
|
|
"XPM is a portable image format "
|
|
|
|
"designed to be included in C source "
|
|
|
|
"code. XLib provides utility functions "
|
|
|
|
"to read this format. Newer code should "
|
|
|
|
"however be using gdk-pixbuf-csource "
|
|
|
|
"instead. XPM supports colored images, "
|
|
|
|
"unlike the XBM format which XPM was "
|
|
|
|
"designed to replace.",
|
|
|
|
name);
|
|
|
|
gimp_procedure_set_attribution (procedure,
|
|
|
|
"Spencer Kimball & Peter Mattis & "
|
|
|
|
"Ray Lehtiniemi",
|
|
|
|
"Spencer Kimball & Peter Mattis",
|
|
|
|
"1997");
|
|
|
|
|
|
|
|
gimp_file_procedure_set_mime_types (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"image/x-pixmap");
|
|
|
|
gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"xpm");
|
|
|
|
gimp_file_procedure_set_magics (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"0, string,/*\\040XPM\\040*/");
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
2019-08-25 00:56:03 +08:00
|
|
|
else if (! strcmp (name, SAVE_PROC))
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2019-08-30 18:52:28 +08:00
|
|
|
procedure = gimp_save_procedure_new (plug_in, name,
|
|
|
|
GIMP_PDB_PROC_TYPE_PLUGIN,
|
2019-08-25 00:56:03 +08:00
|
|
|
xpm_save, NULL, NULL);
|
|
|
|
|
|
|
|
gimp_procedure_set_image_types (procedure, "*");
|
|
|
|
|
|
|
|
gimp_procedure_set_menu_label (procedure, N_("X PixMap image"));
|
|
|
|
|
|
|
|
gimp_procedure_set_documentation (procedure,
|
|
|
|
"Export files in XPM (X11 Pixmap) format.",
|
|
|
|
"Export files in XPM (X11 Pixmap) format. "
|
|
|
|
"XPM is a portable image format "
|
|
|
|
"designed to be included in C source "
|
|
|
|
"code. XLib provides utility functions "
|
|
|
|
"to read this format. Newer code should "
|
|
|
|
"however be using gdk-pixbuf-csource "
|
|
|
|
"instead. XPM supports colored images, "
|
|
|
|
"unlike the XBM format which XPM was "
|
|
|
|
"designed to replace.",
|
|
|
|
name);
|
|
|
|
gimp_procedure_set_attribution (procedure,
|
|
|
|
"Spencer Kimball & Peter Mattis & "
|
|
|
|
"Ray Lehtiniemi & Nathan Summers",
|
|
|
|
"Spencer Kimball & Peter Mattis",
|
|
|
|
"1997");
|
|
|
|
|
|
|
|
gimp_file_procedure_set_mime_types (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"image/x-pixmap");
|
|
|
|
gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"xpm");
|
|
|
|
|
|
|
|
GIMP_PROC_ARG_INT (procedure, "threshold",
|
|
|
|
"Threshold",
|
|
|
|
"Alpha threshold",
|
|
|
|
0, 255, 127,
|
|
|
|
G_PARAM_READWRITE);
|
|
|
|
}
|
2019-08-19 18:05:12 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
return procedure;
|
|
|
|
}
|
1999-10-04 07:48:33 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
static GimpValueArray *
|
|
|
|
xpm_load (GimpProcedure *procedure,
|
|
|
|
GimpRunMode run_mode,
|
|
|
|
GFile *file,
|
|
|
|
const GimpValueArray *args,
|
|
|
|
gpointer run_data)
|
|
|
|
{
|
|
|
|
GimpValueArray *return_vals;
|
|
|
|
GimpImage *image;
|
|
|
|
GError *error = NULL;
|
2000-01-26 01:46:56 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
INIT_I18N ();
|
|
|
|
gegl_init (NULL, NULL);
|
1999-10-04 07:48:33 +08:00
|
|
|
|
2019-09-12 03:48:34 +08:00
|
|
|
image = load_image (file, &error);
|
2003-12-10 22:58:00 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
if (! image)
|
|
|
|
return gimp_procedure_new_return_values (procedure,
|
|
|
|
GIMP_PDB_EXECUTION_ERROR,
|
|
|
|
error);
|
2003-12-10 22:58:00 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
return_vals = gimp_procedure_new_return_values (procedure,
|
|
|
|
GIMP_PDB_SUCCESS,
|
|
|
|
NULL);
|
2003-12-10 22:58:00 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
GIMP_VALUES_SET_IMAGE (return_vals, 1, image);
|
2000-01-26 01:46:56 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
return return_vals;
|
|
|
|
}
|
1999-10-04 07:48:33 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
static GimpValueArray *
|
|
|
|
xpm_save (GimpProcedure *procedure,
|
|
|
|
GimpRunMode run_mode,
|
|
|
|
GimpImage *image,
|
2020-04-14 17:46:17 +08:00
|
|
|
gint n_drawables,
|
|
|
|
GimpDrawable **drawables,
|
2019-08-25 00:56:03 +08:00
|
|
|
GFile *file,
|
|
|
|
const GimpValueArray *args,
|
|
|
|
gpointer run_data)
|
|
|
|
{
|
2019-09-26 18:09:41 +08:00
|
|
|
GimpProcedureConfig *config;
|
|
|
|
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
|
|
|
|
GimpExportReturn export = GIMP_EXPORT_CANCEL;
|
|
|
|
GError *error = NULL;
|
2019-08-25 00:56:03 +08:00
|
|
|
|
|
|
|
INIT_I18N ();
|
|
|
|
gegl_init (NULL, NULL);
|
|
|
|
|
2019-09-26 18:09:41 +08:00
|
|
|
config = gimp_procedure_create_config (procedure);
|
|
|
|
gimp_procedure_config_begin_run (config, image, run_mode, args);
|
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
switch (run_mode)
|
|
|
|
{
|
|
|
|
case GIMP_RUN_INTERACTIVE:
|
|
|
|
case GIMP_RUN_WITH_LAST_VALS:
|
2019-09-21 01:39:00 +08:00
|
|
|
gimp_ui_init (PLUG_IN_BINARY);
|
2019-08-25 00:56:03 +08:00
|
|
|
|
2020-04-14 17:46:17 +08:00
|
|
|
export = gimp_export_image (&image, &n_drawables, &drawables, "XPM",
|
2019-08-25 00:56:03 +08:00
|
|
|
GIMP_EXPORT_CAN_HANDLE_RGB |
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_GRAY |
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_INDEXED |
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_ALPHA);
|
|
|
|
|
|
|
|
if (export == GIMP_EXPORT_CANCEL)
|
|
|
|
return gimp_procedure_new_return_values (procedure,
|
|
|
|
GIMP_PDB_CANCEL,
|
|
|
|
NULL);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
2019-08-25 00:56:03 +08:00
|
|
|
|
2020-04-14 17:46:17 +08:00
|
|
|
if (n_drawables != 1)
|
|
|
|
{
|
|
|
|
g_set_error (&error, G_FILE_ERROR, 0,
|
|
|
|
_("XPM format does not support multiple layers."));
|
|
|
|
|
|
|
|
return gimp_procedure_new_return_values (procedure,
|
|
|
|
GIMP_PDB_CALLING_ERROR,
|
|
|
|
error);
|
|
|
|
}
|
|
|
|
|
2019-09-26 18:09:41 +08:00
|
|
|
if (run_mode == GIMP_RUN_INTERACTIVE)
|
2000-01-26 01:46:56 +08:00
|
|
|
{
|
2020-04-14 17:46:17 +08:00
|
|
|
if (gimp_drawable_has_alpha (drawables[0]))
|
2019-09-26 18:09:41 +08:00
|
|
|
if (! save_dialog (procedure, G_OBJECT (config)))
|
2019-08-25 00:56:03 +08:00
|
|
|
status = GIMP_PDB_CANCEL;
|
2000-01-26 01:46:56 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
if (status == GIMP_PDB_SUCCESS)
|
2008-08-19 03:08:54 +08:00
|
|
|
{
|
2020-04-14 17:46:17 +08:00
|
|
|
if (! save_image (file, image, drawables[0], G_OBJECT (config),
|
2019-09-26 18:09:41 +08:00
|
|
|
&error))
|
2019-08-25 00:56:03 +08:00
|
|
|
{
|
|
|
|
status = GIMP_PDB_EXECUTION_ERROR;
|
|
|
|
}
|
2008-08-19 03:08:54 +08:00
|
|
|
}
|
|
|
|
|
2019-09-26 18:09:41 +08:00
|
|
|
gimp_procedure_config_end_run (config, status);
|
|
|
|
g_object_unref (config);
|
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
if (export == GIMP_EXPORT_EXPORT)
|
2020-04-14 17:46:17 +08:00
|
|
|
{
|
|
|
|
gimp_image_delete (image);
|
|
|
|
g_free (drawables);
|
|
|
|
}
|
2019-08-25 00:56:03 +08:00
|
|
|
|
|
|
|
return gimp_procedure_new_return_values (procedure, status, error);
|
2000-01-26 01:46:56 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
static GimpImage *
|
2019-09-12 03:48:34 +08:00
|
|
|
load_image (GFile *file,
|
|
|
|
GError **error)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2019-08-25 00:56:03 +08:00
|
|
|
XpmImage xpm_image;
|
|
|
|
guchar *cmap;
|
|
|
|
GimpImage *image;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2005-09-30 16:16:10 +08:00
|
|
|
gimp_progress_init_printf (_("Opening '%s'"),
|
2019-09-12 03:48:34 +08:00
|
|
|
gimp_file_get_utf8_name (file));
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* read the raw file */
|
2021-10-02 00:14:14 +08:00
|
|
|
switch (XpmReadFileToXpmImage (g_file_peek_path (file), &xpm_image, NULL))
|
2003-10-16 21:14:57 +08:00
|
|
|
{
|
|
|
|
case XpmSuccess:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XpmOpenFailed:
|
2008-08-19 03:08:54 +08:00
|
|
|
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
|
|
|
|
_("Error opening file '%s'"),
|
2019-09-12 03:48:34 +08:00
|
|
|
gimp_file_get_utf8_name (file));
|
2019-08-25 00:56:03 +08:00
|
|
|
return NULL;
|
2003-10-16 21:14:57 +08:00
|
|
|
|
|
|
|
case XpmFileInvalid:
|
2008-08-19 03:08:54 +08:00
|
|
|
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
|
|
|
|
"%s", _("XPM file invalid"));
|
2019-08-25 00:56:03 +08:00
|
|
|
return NULL;
|
2003-10-16 21:14:57 +08:00
|
|
|
|
|
|
|
default:
|
2019-08-25 00:56:03 +08:00
|
|
|
return NULL;
|
2003-10-16 21:14:57 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2002-06-30 05:01:33 +08:00
|
|
|
cmap = parse_colors (&xpm_image);
|
2000-01-26 01:46:56 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
image = gimp_image_new (xpm_image.width,
|
|
|
|
xpm_image.height,
|
|
|
|
GIMP_RGB);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2019-09-12 03:48:34 +08:00
|
|
|
gimp_image_set_file (image, file);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* fill it */
|
2019-08-25 00:56:03 +08:00
|
|
|
parse_image (image, &xpm_image, cmap);
|
2003-10-16 21:14:57 +08:00
|
|
|
|
|
|
|
g_free (cmap);
|
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
return image;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2003-10-16 21:14:57 +08:00
|
|
|
static guchar *
|
2019-08-25 00:56:03 +08:00
|
|
|
parse_colors (XpmImage *xpm_image)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2003-07-27 17:46:38 +08:00
|
|
|
#ifndef XPM_NO_X
|
1997-11-25 06:05:25 +08:00
|
|
|
Display *display;
|
|
|
|
Colormap colormap;
|
2003-07-27 17:46:38 +08:00
|
|
|
#endif
|
2000-04-14 16:59:52 +08:00
|
|
|
gint i, j;
|
2002-06-30 05:01:33 +08:00
|
|
|
guchar *cmap;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-07-27 17:46:38 +08:00
|
|
|
#ifndef XPM_NO_X
|
1997-11-25 06:05:25 +08:00
|
|
|
/* open the display and get the default color map */
|
|
|
|
display = XOpenDisplay (NULL);
|
2018-03-27 03:25:10 +08:00
|
|
|
if (display == NULL)
|
|
|
|
g_printerr ("Could not open display\n");
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
colormap = DefaultColormap (display, DefaultScreen (display));
|
2003-07-27 17:46:38 +08:00
|
|
|
#endif
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* alloc a buffer to hold the parsed colors */
|
2002-06-30 05:01:33 +08:00
|
|
|
cmap = g_new0 (guchar, 4 * xpm_image->ncolors);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2002-06-30 05:01:33 +08:00
|
|
|
/* parse each color in the file */
|
|
|
|
for (i = 0, j = 0; i < xpm_image->ncolors; i++)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2002-06-30 05:01:33 +08:00
|
|
|
gchar *colorspec = "None";
|
|
|
|
XpmColor *xpm_color;
|
2003-07-27 17:46:38 +08:00
|
|
|
#ifndef XPM_NO_X
|
2002-06-30 05:01:33 +08:00
|
|
|
XColor xcolor;
|
2003-07-27 17:46:38 +08:00
|
|
|
#else
|
|
|
|
GdkColor xcolor;
|
|
|
|
#endif
|
2003-10-16 21:14:57 +08:00
|
|
|
|
2002-06-30 05:01:33 +08:00
|
|
|
xpm_color = &(xpm_image->colorTable[i]);
|
2003-10-16 21:14:57 +08:00
|
|
|
|
2002-06-30 05:01:33 +08:00
|
|
|
/* pick the best spec available */
|
|
|
|
if (xpm_color->c_color)
|
2003-12-10 22:58:00 +08:00
|
|
|
colorspec = xpm_color->c_color;
|
2002-06-30 05:01:33 +08:00
|
|
|
else if (xpm_color->g_color)
|
2003-12-10 22:58:00 +08:00
|
|
|
colorspec = xpm_color->g_color;
|
2002-06-30 05:01:33 +08:00
|
|
|
else if (xpm_color->g4_color)
|
2003-12-10 22:58:00 +08:00
|
|
|
colorspec = xpm_color->g4_color;
|
2002-06-30 05:01:33 +08:00
|
|
|
else if (xpm_color->m_color)
|
2003-12-10 22:58:00 +08:00
|
|
|
colorspec = xpm_color->m_color;
|
2003-10-16 21:14:57 +08:00
|
|
|
|
2004-11-20 04:00:22 +08:00
|
|
|
/* parse if it's not transparent */
|
2002-06-30 05:01:33 +08:00
|
|
|
if (strcmp (colorspec, "None") != 0)
|
2003-12-10 22:58:00 +08:00
|
|
|
{
|
2003-07-27 17:46:38 +08:00
|
|
|
#ifndef XPM_NO_X
|
2003-12-10 22:58:00 +08:00
|
|
|
XParseColor (display, colormap, colorspec, &xcolor);
|
2003-07-27 17:46:38 +08:00
|
|
|
#else
|
2003-12-10 22:58:00 +08:00
|
|
|
gdk_color_parse (colorspec, &xcolor);
|
2003-07-27 17:46:38 +08:00
|
|
|
#endif
|
2003-12-10 22:58:00 +08:00
|
|
|
cmap[j++] = xcolor.red >> 8;
|
|
|
|
cmap[j++] = xcolor.green >> 8;
|
|
|
|
cmap[j++] = xcolor.blue >> 8;
|
|
|
|
cmap[j++] = ~0;
|
|
|
|
}
|
2002-06-30 05:01:33 +08:00
|
|
|
else
|
2003-12-10 22:58:00 +08:00
|
|
|
{
|
|
|
|
j += 4;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
2003-07-27 17:46:38 +08:00
|
|
|
#ifndef XPM_NO_X
|
1997-11-25 06:05:25 +08:00
|
|
|
XCloseDisplay (display);
|
2003-07-27 17:46:38 +08:00
|
|
|
#endif
|
2002-06-30 05:01:33 +08:00
|
|
|
return cmap;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-08-25 00:56:03 +08:00
|
|
|
parse_image (GimpImage *image,
|
|
|
|
XpmImage *xpm_image,
|
|
|
|
guchar *cmap)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2012-11-26 09:39:44 +08:00
|
|
|
GeglBuffer *buffer;
|
|
|
|
gint tile_height;
|
|
|
|
gint scanlines;
|
|
|
|
gint val;
|
|
|
|
guchar *buf;
|
|
|
|
guchar *dest;
|
|
|
|
guint *src;
|
2019-08-25 00:56:03 +08:00
|
|
|
GimpLayer *layer;
|
2012-11-26 09:39:44 +08:00
|
|
|
gint i;
|
2003-10-16 21:14:57 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
layer = gimp_layer_new (image,
|
|
|
|
_("Color"),
|
|
|
|
xpm_image->width,
|
|
|
|
xpm_image->height,
|
|
|
|
GIMP_RGBA_IMAGE,
|
|
|
|
100,
|
|
|
|
gimp_image_get_default_new_layer_mode (image));
|
2003-10-16 21:14:57 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
gimp_image_insert_layer (image, layer, NULL, 0);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
buffer = gimp_drawable_get_buffer (GIMP_DRAWABLE (layer));
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
tile_height = gimp_tile_height ();
|
2003-10-16 21:14:57 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
buf = g_new (guchar, tile_height * xpm_image->width * 4);
|
|
|
|
|
2002-06-30 05:01:33 +08:00
|
|
|
src = xpm_image->data;
|
2012-11-26 09:39:44 +08:00
|
|
|
for (i = 0; i < xpm_image->height; i += tile_height)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2012-11-26 09:39:44 +08:00
|
|
|
gint j;
|
|
|
|
|
2002-06-30 05:01:33 +08:00
|
|
|
dest = buf;
|
2012-11-26 09:39:44 +08:00
|
|
|
scanlines = MIN (tile_height, xpm_image->height - i);
|
2002-06-30 05:01:33 +08:00
|
|
|
j = scanlines * xpm_image->width;
|
2012-11-26 09:39:44 +08:00
|
|
|
while (j--)
|
2003-12-10 22:58:00 +08:00
|
|
|
{
|
2012-11-26 09:39:44 +08:00
|
|
|
{
|
|
|
|
val = *(src++) * 4;
|
|
|
|
*(dest) = cmap[val];
|
|
|
|
*(dest+1) = cmap[val+1];
|
|
|
|
*(dest+2) = cmap[val+2];
|
|
|
|
*(dest+3) = cmap[val+3];
|
|
|
|
dest += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((j % 100) == 0)
|
|
|
|
gimp_progress_update ((double) i / (double) xpm_image->height);
|
2002-06-30 05:01:33 +08:00
|
|
|
}
|
2003-10-16 21:14:57 +08:00
|
|
|
|
2012-11-26 09:39:44 +08:00
|
|
|
gegl_buffer_set (buffer,
|
|
|
|
GEGL_RECTANGLE (0, i, xpm_image->width, scanlines), 0,
|
|
|
|
NULL, buf, GEGL_AUTO_ROWSTRIDE);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
2003-10-16 21:14:57 +08:00
|
|
|
|
2012-11-26 09:39:44 +08:00
|
|
|
g_free (buf);
|
|
|
|
g_object_unref (buffer);
|
1998-02-25 14:21:26 +08:00
|
|
|
|
2012-11-26 09:39:44 +08:00
|
|
|
gimp_progress_update (1.0);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2002-06-30 05:01:33 +08:00
|
|
|
static guint
|
2000-01-26 01:46:56 +08:00
|
|
|
rgbhash (rgbkey *c)
|
1998-02-25 14:21:26 +08:00
|
|
|
{
|
1999-10-21 08:11:37 +08:00
|
|
|
return ((guint)c->r) ^ ((guint)c->g) ^ ((guint)c->b);
|
1998-02-25 14:21:26 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2002-06-30 05:01:33 +08:00
|
|
|
static guint
|
2003-10-16 21:14:57 +08:00
|
|
|
compare (rgbkey *c1,
|
2003-12-10 22:58:00 +08:00
|
|
|
rgbkey *c2)
|
1998-02-25 14:21:26 +08:00
|
|
|
{
|
2000-04-14 16:59:52 +08:00
|
|
|
return (c1->r == c2->r) && (c1->g == c2->g) && (c1->b == c2->b);
|
1998-02-25 14:21:26 +08:00
|
|
|
}
|
2002-06-30 05:01:33 +08:00
|
|
|
|
|
|
|
static void
|
2003-10-16 21:14:57 +08:00
|
|
|
set_XpmImage (XpmColor *array,
|
2003-12-10 22:58:00 +08:00
|
|
|
guint index,
|
|
|
|
gchar *colorstring)
|
1998-02-25 14:21:26 +08:00
|
|
|
{
|
2000-01-26 01:46:56 +08:00
|
|
|
gchar *p;
|
2012-11-26 09:39:44 +08:00
|
|
|
gint i, charnum, indtemp;
|
2003-10-16 21:14:57 +08:00
|
|
|
|
1999-10-21 08:11:37 +08:00
|
|
|
indtemp=index;
|
2000-04-14 16:59:52 +08:00
|
|
|
array[index].string = p = g_new (gchar, cpp+1);
|
2003-10-16 21:14:57 +08:00
|
|
|
|
1999-10-21 08:11:37 +08:00
|
|
|
/*convert the index number to base sizeof(linenoise)-1 */
|
2019-08-25 00:56:03 +08:00
|
|
|
for (i = 0; i < cpp; ++i)
|
1999-10-21 08:11:37 +08:00
|
|
|
{
|
2000-04-14 16:59:52 +08:00
|
|
|
charnum = indtemp % (sizeof (linenoise) - 1);
|
|
|
|
indtemp = indtemp / (sizeof (linenoise) - 1);
|
|
|
|
*p++ = linenoise[charnum];
|
1999-10-21 08:11:37 +08:00
|
|
|
}
|
|
|
|
/* *p++=linenoise[indtemp]; */
|
2003-10-16 21:14:57 +08:00
|
|
|
|
2000-04-14 16:59:52 +08:00
|
|
|
*p = '\0'; /* C and its stupid null-terminated strings... */
|
1999-10-21 08:11:37 +08:00
|
|
|
|
|
|
|
array[index].symbolic = NULL;
|
2000-04-14 16:59:52 +08:00
|
|
|
array[index].m_color = NULL;
|
1999-10-21 08:11:37 +08:00
|
|
|
array[index].g4_color = NULL;
|
2000-04-14 16:59:52 +08:00
|
|
|
|
1999-10-21 08:11:37 +08:00
|
|
|
if (color)
|
|
|
|
{
|
|
|
|
array[index].g_color = NULL;
|
|
|
|
array[index].c_color = colorstring;
|
2008-09-17 16:37:24 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-10-21 08:11:37 +08:00
|
|
|
array[index].c_color = NULL;
|
|
|
|
array[index].g_color = colorstring;
|
|
|
|
}
|
1998-02-25 14:21:26 +08:00
|
|
|
}
|
|
|
|
|
2002-06-30 05:01:33 +08:00
|
|
|
static void
|
2003-10-16 21:14:57 +08:00
|
|
|
create_colormap_from_hash (gpointer gkey,
|
2003-12-10 22:58:00 +08:00
|
|
|
gpointer value,
|
|
|
|
gpointer user_data)
|
1998-02-25 14:21:26 +08:00
|
|
|
{
|
2003-03-11 05:07:05 +08:00
|
|
|
rgbkey *key = gkey;
|
|
|
|
gchar *string = g_new (gchar, 8);
|
1999-10-21 08:11:37 +08:00
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
sprintf (string, "#%02X%02X%02X", (int)key->r, (int)key->g, (int)key->b);
|
|
|
|
set_XpmImage (user_data, *((int *) value), string);
|
1998-02-25 14:21:26 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2020-02-02 01:00:07 +08:00
|
|
|
static void
|
|
|
|
decrement_hash_values (gpointer gkey,
|
|
|
|
gpointer value,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
--(*((guint*) value));
|
|
|
|
}
|
|
|
|
|
2000-04-14 16:59:52 +08:00
|
|
|
static gboolean
|
2019-09-12 03:48:34 +08:00
|
|
|
save_image (GFile *file,
|
2019-08-25 00:56:03 +08:00
|
|
|
GimpImage *image,
|
|
|
|
GimpDrawable *drawable,
|
2019-09-26 18:09:41 +08:00
|
|
|
GObject *config,
|
2019-08-25 00:56:03 +08:00
|
|
|
GError **error)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2012-11-26 09:39:44 +08:00
|
|
|
GeglBuffer *buffer;
|
|
|
|
const Babl *format;
|
|
|
|
gint width;
|
|
|
|
gint height;
|
|
|
|
gint ncolors = 1;
|
|
|
|
gint *indexno;
|
|
|
|
gboolean indexed;
|
|
|
|
gboolean alpha;
|
2020-02-02 01:00:07 +08:00
|
|
|
gboolean alpha_used = FALSE;
|
2012-11-26 09:39:44 +08:00
|
|
|
XpmColor *colormap;
|
2019-08-25 00:56:03 +08:00
|
|
|
XpmImage *xpm_image;
|
2012-11-26 09:39:44 +08:00
|
|
|
guint *ibuff = NULL;
|
|
|
|
guchar *buf;
|
|
|
|
guchar *data;
|
1998-02-25 14:21:26 +08:00
|
|
|
GHashTable *hash = NULL;
|
2012-11-26 09:39:44 +08:00
|
|
|
gint i, j, k;
|
2019-09-26 18:09:41 +08:00
|
|
|
gint threshold;
|
2012-11-26 09:39:44 +08:00
|
|
|
gboolean success = FALSE;
|
2000-01-26 01:46:56 +08:00
|
|
|
|
2019-09-26 18:09:41 +08:00
|
|
|
g_object_get (config,
|
|
|
|
"threshold", &threshold,
|
|
|
|
NULL);
|
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
buffer = gimp_drawable_get_buffer (drawable);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2012-11-26 09:39:44 +08:00
|
|
|
width = gegl_buffer_get_width (buffer);
|
|
|
|
height = gegl_buffer_get_height (buffer);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
alpha = gimp_drawable_has_alpha (drawable);
|
|
|
|
color = ! gimp_drawable_is_gray (drawable);
|
|
|
|
indexed = gimp_drawable_is_indexed (drawable);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
switch (gimp_drawable_type (drawable))
|
2012-11-26 09:39:44 +08:00
|
|
|
{
|
|
|
|
case GIMP_RGB_IMAGE:
|
|
|
|
format = babl_format ("R'G'B' u8");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_RGBA_IMAGE:
|
|
|
|
format = babl_format ("R'G'B'A u8");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_GRAY_IMAGE:
|
|
|
|
format = babl_format ("Y' u8");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_GRAYA_IMAGE:
|
|
|
|
format = babl_format ("Y'A u8");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_INDEXED_IMAGE:
|
|
|
|
case GIMP_INDEXEDA_IMAGE:
|
|
|
|
format = gegl_buffer_get_format (buffer);
|
|
|
|
break;
|
2013-05-13 19:07:48 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
|
|
|
|
_("Unsupported drawable type"));
|
|
|
|
g_object_unref (buffer);
|
|
|
|
return FALSE;
|
2012-11-26 09:39:44 +08:00
|
|
|
}
|
2003-10-16 21:14:57 +08:00
|
|
|
|
2003-03-11 05:07:05 +08:00
|
|
|
/* allocate buffer making the assumption that ibuff is 32 bit aligned... */
|
2004-11-20 04:00:22 +08:00
|
|
|
ibuff = g_new (guint, width * height);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2004-11-20 04:00:22 +08:00
|
|
|
hash = g_hash_table_new ((GHashFunc) rgbhash, (GCompareFunc) compare);
|
2003-10-16 21:14:57 +08:00
|
|
|
|
2016-02-16 09:35:43 +08:00
|
|
|
gimp_progress_init_printf (_("Exporting '%s'"),
|
2019-09-12 03:48:34 +08:00
|
|
|
gimp_file_get_utf8_name (file));
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-03-11 05:07:05 +08:00
|
|
|
ncolors = alpha ? 1 : 0;
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* allocate a pixel region to work with */
|
2012-11-26 09:39:44 +08:00
|
|
|
buf = g_new (guchar,
|
|
|
|
gimp_tile_height () * width *
|
|
|
|
babl_format_get_bytes_per_pixel (format));
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* process each row of tiles */
|
2012-11-26 09:39:44 +08:00
|
|
|
for (i = 0; i < height; i += gimp_tile_height ())
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-04-14 16:59:52 +08:00
|
|
|
gint scanlines;
|
2002-06-30 05:01:33 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* read the next row of tiles */
|
2000-04-14 16:59:52 +08:00
|
|
|
scanlines = MIN (gimp_tile_height(), height - i);
|
2012-11-26 09:39:44 +08:00
|
|
|
|
|
|
|
gegl_buffer_get (buffer, GEGL_RECTANGLE (0, i, width, scanlines), 1.0,
|
|
|
|
format, buf,
|
|
|
|
GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
|
|
|
|
|
|
|
|
data = buf;
|
2003-10-16 21:14:57 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* process each pixel row */
|
2004-11-20 04:00:22 +08:00
|
|
|
for (j = 0; j < scanlines; j++)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
|
|
|
/* go to the start of this row in each image */
|
1998-02-25 14:21:26 +08:00
|
|
|
guint *idata = ibuff + (i+j) * width;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* do each pixel in the row */
|
2004-11-20 04:00:22 +08:00
|
|
|
for (k = 0; k < width; k++)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2011-10-12 20:55:17 +08:00
|
|
|
rgbkey *key = g_new (rgbkey, 1);
|
2003-12-10 22:58:00 +08:00
|
|
|
guchar a;
|
2003-10-16 21:14:57 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* get pixel data */
|
1998-02-25 14:21:26 +08:00
|
|
|
key->r = *(data++);
|
2003-12-10 22:58:00 +08:00
|
|
|
key->g = color && !indexed ? *(data++) : key->r;
|
|
|
|
key->b = color && !indexed ? *(data++) : key->r;
|
|
|
|
a = alpha ? *(data++) : 255;
|
|
|
|
|
|
|
|
if (a < threshold)
|
|
|
|
{
|
|
|
|
*(idata++) = 0;
|
2020-02-02 01:00:07 +08:00
|
|
|
alpha_used = TRUE;
|
2003-12-10 22:58:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (indexed)
|
|
|
|
{
|
|
|
|
*(idata++) = (key->r) + (alpha ? 1 : 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
indexno = g_hash_table_lookup (hash, key);
|
|
|
|
if (!indexno)
|
|
|
|
{
|
|
|
|
indexno = g_new (gint, 1);
|
|
|
|
*indexno = ncolors++;
|
|
|
|
g_hash_table_insert (hash, key, indexno);
|
2011-10-12 20:55:17 +08:00
|
|
|
key = g_new (rgbkey, 1);
|
2003-12-10 22:58:00 +08:00
|
|
|
}
|
|
|
|
*(idata++) = *indexno;
|
|
|
|
}
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
2000-01-26 01:46:56 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* kick the progress bar */
|
2000-04-14 16:59:52 +08:00
|
|
|
gimp_progress_update ((gdouble) (i+j) / (gdouble) height);
|
2003-10-16 21:14:57 +08:00
|
|
|
}
|
|
|
|
}
|
2008-08-19 03:08:54 +08:00
|
|
|
|
2012-11-26 09:39:44 +08:00
|
|
|
g_free (buf);
|
1998-02-25 14:21:26 +08:00
|
|
|
|
2020-02-02 01:00:07 +08:00
|
|
|
/* remove alpha if not actually used */
|
|
|
|
if (alpha && !alpha_used)
|
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
--ncolors;
|
|
|
|
for (i = 0; i < width * height; ++i)
|
|
|
|
--ibuff[i];
|
|
|
|
|
|
|
|
g_hash_table_foreach (hash, decrement_hash_values, NULL);
|
|
|
|
}
|
|
|
|
|
1998-02-25 14:21:26 +08:00
|
|
|
if (indexed)
|
1999-10-04 07:48:33 +08:00
|
|
|
{
|
2019-08-25 00:56:03 +08:00
|
|
|
guchar *cmap = gimp_image_get_colormap (image, &ncolors);
|
2003-03-31 17:34:02 +08:00
|
|
|
guchar *c;
|
|
|
|
|
|
|
|
c = cmap;
|
2003-03-11 05:07:05 +08:00
|
|
|
|
2020-02-02 01:00:07 +08:00
|
|
|
if (alpha_used)
|
2003-12-10 22:58:00 +08:00
|
|
|
ncolors++;
|
2003-03-11 05:07:05 +08:00
|
|
|
|
2000-04-14 16:59:52 +08:00
|
|
|
colormap = g_new (XpmColor, ncolors);
|
2003-10-16 21:14:57 +08:00
|
|
|
cpp =
|
2003-12-10 22:58:00 +08:00
|
|
|
1 + (gdouble) log (ncolors) / (gdouble) log (sizeof (linenoise) - 1.0);
|
2003-03-11 05:07:05 +08:00
|
|
|
|
2020-02-02 01:00:07 +08:00
|
|
|
if (alpha_used)
|
2003-12-10 22:58:00 +08:00
|
|
|
set_XpmImage (colormap, 0, "None");
|
2003-03-11 05:07:05 +08:00
|
|
|
|
2020-02-02 01:00:07 +08:00
|
|
|
for (i = alpha_used ? 1 : 0; i < ncolors; i++)
|
2003-12-10 22:58:00 +08:00
|
|
|
{
|
|
|
|
gchar *string;
|
|
|
|
guchar r, g, b;
|
2003-03-11 05:07:05 +08:00
|
|
|
|
2003-12-10 22:58:00 +08:00
|
|
|
r = *c++;
|
|
|
|
g = *c++;
|
|
|
|
b = *c++;
|
2003-03-11 05:07:05 +08:00
|
|
|
|
2003-12-10 22:58:00 +08:00
|
|
|
string = g_new (gchar, 8);
|
|
|
|
sprintf (string, "#%02X%02X%02X", (int)r, (int)g, (int)b);
|
|
|
|
set_XpmImage (colormap, i, string);
|
|
|
|
}
|
2003-03-11 05:07:05 +08:00
|
|
|
|
|
|
|
g_free (cmap);
|
1999-10-04 07:48:33 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-04-14 16:59:52 +08:00
|
|
|
colormap = g_new (XpmColor, ncolors);
|
2003-03-11 05:07:05 +08:00
|
|
|
cpp =
|
2003-12-10 22:58:00 +08:00
|
|
|
1 + (gdouble) log (ncolors) / (gdouble) log (sizeof (linenoise) - 1.0);
|
2003-03-11 05:07:05 +08:00
|
|
|
|
2020-02-02 01:00:07 +08:00
|
|
|
if (alpha_used)
|
2003-12-10 22:58:00 +08:00
|
|
|
set_XpmImage (colormap, 0, "None");
|
2003-03-11 05:07:05 +08:00
|
|
|
|
1999-10-04 07:48:33 +08:00
|
|
|
g_hash_table_foreach (hash, create_colormap_from_hash, colormap);
|
|
|
|
}
|
2000-01-26 01:46:56 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
xpm_image = g_new (XpmImage, 1);
|
2003-10-16 21:14:57 +08:00
|
|
|
|
2019-08-25 00:56:03 +08:00
|
|
|
xpm_image->width = width;
|
|
|
|
xpm_image->height = height;
|
|
|
|
xpm_image->ncolors = ncolors;
|
|
|
|
xpm_image->cpp = cpp;
|
|
|
|
xpm_image->colorTable = colormap;
|
|
|
|
xpm_image->data = ibuff;
|
2003-10-16 21:14:57 +08:00
|
|
|
|
1999-10-04 07:48:33 +08:00
|
|
|
/* do the save */
|
2021-10-02 00:14:14 +08:00
|
|
|
switch (XpmWriteFileFromXpmImage (g_file_peek_path (file), xpm_image, NULL))
|
2008-08-19 03:08:54 +08:00
|
|
|
{
|
|
|
|
case XpmSuccess:
|
|
|
|
success = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XpmOpenFailed:
|
|
|
|
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
|
|
|
|
_("Error opening file '%s'"),
|
2019-09-12 03:48:34 +08:00
|
|
|
gimp_file_get_utf8_name (file));
|
2008-08-19 03:08:54 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case XpmFileInvalid:
|
|
|
|
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
|
|
|
|
"%s", _("XPM file invalid"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2000-01-26 01:46:56 +08:00
|
|
|
|
2012-11-26 09:39:44 +08:00
|
|
|
g_object_unref (buffer);
|
2002-06-30 05:01:33 +08:00
|
|
|
g_free (ibuff);
|
2003-03-11 05:07:05 +08:00
|
|
|
|
2003-10-16 21:14:57 +08:00
|
|
|
if (hash)
|
2000-04-14 16:59:52 +08:00
|
|
|
g_hash_table_destroy (hash);
|
2003-10-16 21:14:57 +08:00
|
|
|
|
2012-11-26 09:39:44 +08:00
|
|
|
gimp_progress_update (1.0);
|
|
|
|
|
2008-08-19 03:08:54 +08:00
|
|
|
return success;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2004-05-20 09:19:47 +08:00
|
|
|
static gboolean
|
2019-09-26 18:09:41 +08:00
|
|
|
save_dialog (GimpProcedure *procedure,
|
|
|
|
GObject *config)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2019-09-26 18:09:41 +08:00
|
|
|
GtkWidget *dialog;
|
2020-10-30 07:00:28 +08:00
|
|
|
GtkWidget *scale;
|
2019-09-26 18:09:41 +08:00
|
|
|
gboolean run;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2019-09-26 18:09:41 +08:00
|
|
|
dialog = gimp_procedure_dialog_new (procedure,
|
|
|
|
GIMP_PROCEDURE_CONFIG (config),
|
|
|
|
_("Export Image as XPM"));
|
2005-09-10 02:38:00 +08:00
|
|
|
|
2020-11-25 09:19:39 +08:00
|
|
|
scale = gimp_prop_scale_entry_new (config, "threshold", NULL, 1.0, FALSE, 0, 0);
|
2019-09-26 18:09:41 +08:00
|
|
|
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
|
2020-10-30 07:00:28 +08:00
|
|
|
scale, TRUE, TRUE, 6);
|
|
|
|
gtk_widget_show (scale);
|
2005-09-10 02:38:00 +08:00
|
|
|
gtk_widget_show (dialog);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2019-09-26 18:09:41 +08:00
|
|
|
run = gimp_procedure_dialog_run (GIMP_PROCEDURE_DIALOG (dialog));
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2005-09-10 02:38:00 +08:00
|
|
|
gtk_widget_destroy (dialog);
|
2000-01-11 23:48:00 +08:00
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
return run;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|