2006-12-09 21:33:38 +00:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
1997-11-24 22:05:25 +00:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2009-01-17 22:28:01 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
1997-11-24 22:05:25 +00:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-17 22:28:01 +00:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
1997-11-24 22:05:25 +00: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-11 23:27:07 +02:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
1997-11-24 22:05:25 +00:00
|
|
|
*/
|
|
|
|
|
2003-03-31 09:34:02 +00:00
|
|
|
/* XPM plugin version 1.2.6 */
|
1998-03-17 00:31:58 +00:00
|
|
|
|
2000-04-14 08:59:52 +00:00
|
|
|
/*
|
2003-03-31 09:34:02 +00:00
|
|
|
1.2.6 fixes crash when saving indexed images (bug #109567)
|
|
|
|
|
2003-03-10 21:07:05 +00:00
|
|
|
1.2.5 only creates a "None" color entry if the image has alpha (bug #108034)
|
|
|
|
|
2002-07-09 16:17:43 +00:00
|
|
|
1.2.4 displays an error message if saving fails (bug #87588)
|
|
|
|
|
2000-04-14 08:59:52 +00: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 00:31:58 +00: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-24 22:05:25 +00:00
|
|
|
|
1999-05-29 16:35:47 +00:00
|
|
|
#include "config.h"
|
2000-01-11 15:48:00 +00:00
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
#include <string.h>
|
2000-01-11 15:48:00 +00:00
|
|
|
|
2005-03-04 15:12:29 +00:00
|
|
|
#include <glib/gstdio.h>
|
2000-01-11 15:48:00 +00:00
|
|
|
|
2010-12-29 15:19:50 +01:00
|
|
|
#include <gdk/gdk.h> /* For GDK_WINDOWING_WIN32 */
|
2005-05-13 12:50:35 +00:00
|
|
|
|
2006-12-06 11:01:37 +00:00
|
|
|
#ifndef GDK_WINDOWING_X11
|
2003-07-27 09:46:38 +00:00
|
|
|
#ifndef XPM_NO_X
|
|
|
|
#define XPM_NO_X
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <X11/xpm.h>
|
|
|
|
|
2000-01-11 15:48:00 +00:00
|
|
|
#include <libgimp/gimp.h>
|
|
|
|
#include <libgimp/gimpui.h>
|
|
|
|
|
1999-05-29 16:35:47 +00:00
|
|
|
#include "libgimp/stdplugins-intl.h"
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2000-05-01 20:22:55 +00:00
|
|
|
|
2005-08-15 22:42:34 +00:00
|
|
|
#define LOAD_PROC "file-xpm-load"
|
|
|
|
#define SAVE_PROC "file-xpm-save"
|
2008-08-11 10:06:13 +00:00
|
|
|
#define PLUG_IN_BINARY "file-xpm"
|
2011-04-08 20:31:34 +02:00
|
|
|
#define PLUG_IN_ROLE "gimp-file-xpm"
|
2005-08-15 22:42:34 +00:00
|
|
|
#define SCALE_WIDTH 125
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
/* Structs for the save dialog */
|
|
|
|
typedef struct
|
|
|
|
{
|
2000-04-14 08:59:52 +00:00
|
|
|
gint threshold;
|
1997-11-24 22:05:25 +00:00
|
|
|
} XpmSaveVals;
|
|
|
|
|
1998-02-25 06:21:26 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
1999-10-03 23:48:33 +00:00
|
|
|
guchar r;
|
|
|
|
guchar g;
|
|
|
|
guchar b;
|
1998-02-25 06:21:26 +00:00
|
|
|
} rgbkey;
|
|
|
|
|
2019-08-24 18:56:03 +02: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,
|
|
|
|
GimpDrawable *drawable,
|
|
|
|
GFile *file,
|
|
|
|
const GimpValueArray *args,
|
|
|
|
gpointer run_data);
|
|
|
|
|
|
|
|
static GimpImage * load_image (const gchar *filename,
|
|
|
|
GError **error);
|
|
|
|
static guchar * parse_colors (XpmImage *xpm_image);
|
|
|
|
static void parse_image (GimpImage *image,
|
|
|
|
XpmImage *xpm_image,
|
|
|
|
guchar *cmap);
|
|
|
|
static gboolean save_image (const gchar *filename,
|
|
|
|
GimpImage *image,
|
|
|
|
GimpDrawable *drawable,
|
|
|
|
GError **error);
|
|
|
|
static gboolean save_dialog (void);
|
|
|
|
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (Xpm, xpm, GIMP_TYPE_PLUG_IN)
|
|
|
|
|
|
|
|
GIMP_MAIN (XPM_TYPE)
|
|
|
|
|
|
|
|
|
|
|
|
static const gchar linenoise [] =
|
|
|
|
" .+@#$%&*=-;>,')!~{]^/(_:<[}|1234567890abcdefghijklmnopqrstuvwxyz\
|
|
|
|
ABCDEFGHIJKLMNOPQRSTUVWXYZ`";
|
|
|
|
|
2000-01-25 17:46:56 +00:00
|
|
|
/* whether the image is color or not. global so I only have to pass
|
|
|
|
* one user value to the GHFunc
|
|
|
|
*/
|
2002-06-29 21:01:33 +00:00
|
|
|
static gboolean color;
|
2000-01-25 17:46:56 +00:00
|
|
|
|
|
|
|
/* bytes per pixel. global so I only have to pass one user value
|
|
|
|
* to the GHFunc
|
|
|
|
*/
|
2002-06-29 21:01:33 +00:00
|
|
|
static gint cpp;
|
1998-02-25 06:21:26 +00:00
|
|
|
|
2003-10-16 13:14:57 +00:00
|
|
|
static XpmSaveVals xpmvals =
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2000-04-14 08:59:52 +00:00
|
|
|
127 /* alpha threshold */
|
1997-11-24 22:05:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2019-08-24 18:56:03 +02:00
|
|
|
xpm_class_init (XpmClass *klass)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2019-08-24 18:56:03 +02: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-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-08-24 18:56:03 +02:00
|
|
|
xpm_init (Xpm *xpm)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2019-08-24 18:56:03 +02:00
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
static GList *
|
|
|
|
xpm_query_procedures (GimpPlugIn *plug_in)
|
|
|
|
{
|
|
|
|
GList *list = NULL;
|
2012-11-26 02:39:44 +01:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
list = g_list_append (list, g_strdup (LOAD_PROC));
|
|
|
|
list = g_list_append (list, g_strdup (SAVE_PROC));
|
2003-03-25 16:38:19 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
return list;
|
|
|
|
}
|
2002-01-15 18:35:29 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
static GimpProcedure *
|
|
|
|
xpm_create_procedure (GimpPlugIn *plug_in,
|
|
|
|
const gchar *name)
|
|
|
|
{
|
|
|
|
GimpProcedure *procedure = NULL;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
if (! strcmp (name, LOAD_PROC))
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2019-08-30 12:52:28 +02:00
|
|
|
procedure = gimp_load_procedure_new (plug_in, name,
|
|
|
|
GIMP_PDB_PROC_TYPE_PLUGIN,
|
2019-08-24 18:56:03 +02: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-24 22:05:25 +00:00
|
|
|
}
|
2019-08-24 18:56:03 +02:00
|
|
|
else if (! strcmp (name, SAVE_PROC))
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2019-08-30 12:52:28 +02:00
|
|
|
procedure = gimp_save_procedure_new (plug_in, name,
|
|
|
|
GIMP_PDB_PROC_TYPE_PLUGIN,
|
2019-08-24 18:56:03 +02: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 12:05:12 +02:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
return procedure;
|
|
|
|
}
|
1999-10-03 23:48:33 +00:00
|
|
|
|
2019-08-24 18:56:03 +02: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-25 17:46:56 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
INIT_I18N ();
|
|
|
|
gegl_init (NULL, NULL);
|
1999-10-03 23:48:33 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
image = load_image (g_file_get_path (file), &error);
|
2003-12-10 14:58:00 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
if (! image)
|
|
|
|
return gimp_procedure_new_return_values (procedure,
|
|
|
|
GIMP_PDB_EXECUTION_ERROR,
|
|
|
|
error);
|
2003-12-10 14:58:00 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
return_vals = gimp_procedure_new_return_values (procedure,
|
|
|
|
GIMP_PDB_SUCCESS,
|
|
|
|
NULL);
|
2003-12-10 14:58:00 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
GIMP_VALUES_SET_IMAGE (return_vals, 1, image);
|
2000-01-25 17:46:56 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
return return_vals;
|
|
|
|
}
|
1999-10-03 23:48:33 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
static GimpValueArray *
|
|
|
|
xpm_save (GimpProcedure *procedure,
|
|
|
|
GimpRunMode run_mode,
|
|
|
|
GimpImage *image,
|
|
|
|
GimpDrawable *drawable,
|
|
|
|
GFile *file,
|
|
|
|
const GimpValueArray *args,
|
|
|
|
gpointer run_data)
|
|
|
|
{
|
|
|
|
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
|
|
|
|
GimpExportReturn export = GIMP_EXPORT_CANCEL;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
INIT_I18N ();
|
|
|
|
gegl_init (NULL, NULL);
|
|
|
|
|
|
|
|
switch (run_mode)
|
|
|
|
{
|
|
|
|
case GIMP_RUN_INTERACTIVE:
|
|
|
|
case GIMP_RUN_WITH_LAST_VALS:
|
|
|
|
gimp_ui_init (PLUG_IN_BINARY, FALSE);
|
|
|
|
|
|
|
|
export = gimp_export_image (&image, &drawable, "XPM",
|
|
|
|
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-24 22:05:25 +00:00
|
|
|
}
|
2019-08-24 18:56:03 +02:00
|
|
|
|
|
|
|
switch (run_mode)
|
2000-01-25 17:46:56 +00:00
|
|
|
{
|
2019-08-24 18:56:03 +02:00
|
|
|
case GIMP_RUN_INTERACTIVE:
|
|
|
|
gimp_get_data (SAVE_PROC, &xpmvals);
|
|
|
|
|
|
|
|
if (gimp_drawable_has_alpha (drawable))
|
|
|
|
if (! save_dialog ())
|
|
|
|
status = GIMP_PDB_CANCEL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_RUN_NONINTERACTIVE:
|
|
|
|
xpmvals.threshold = GIMP_VALUES_GET_INT (args, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GIMP_RUN_WITH_LAST_VALS:
|
|
|
|
gimp_get_data (SAVE_PROC, &xpmvals);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2000-01-25 17:46:56 +00:00
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
if (status == GIMP_PDB_SUCCESS)
|
2008-08-18 19:08:54 +00:00
|
|
|
{
|
2019-08-24 18:56:03 +02:00
|
|
|
if (save_image (g_file_get_path (file),
|
|
|
|
image, drawable,
|
|
|
|
&error))
|
|
|
|
{
|
|
|
|
gimp_set_data (SAVE_PROC, &xpmvals, sizeof (XpmSaveVals));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = GIMP_PDB_EXECUTION_ERROR;
|
|
|
|
}
|
2008-08-18 19:08:54 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
if (export == GIMP_EXPORT_EXPORT)
|
|
|
|
gimp_image_delete (image);
|
|
|
|
|
|
|
|
return gimp_procedure_new_return_values (procedure, status, error);
|
2000-01-25 17:46:56 +00:00
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
static GimpImage *
|
2008-08-18 19:08:54 +00:00
|
|
|
load_image (const gchar *filename,
|
|
|
|
GError **error)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2019-08-24 18:56:03 +02:00
|
|
|
XpmImage xpm_image;
|
|
|
|
guchar *cmap;
|
|
|
|
GimpImage *image;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2005-09-30 08:16:10 +00:00
|
|
|
gimp_progress_init_printf (_("Opening '%s'"),
|
2005-09-29 18:34:08 +00:00
|
|
|
gimp_filename_to_utf8 (filename));
|
1997-11-24 22:05:25 +00:00
|
|
|
|
|
|
|
/* read the raw file */
|
2003-10-16 13:14:57 +00:00
|
|
|
switch (XpmReadFileToXpmImage ((char *) filename, &xpm_image, NULL))
|
|
|
|
{
|
|
|
|
case XpmSuccess:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XpmOpenFailed:
|
2008-08-18 19:08:54 +00:00
|
|
|
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
|
|
|
|
_("Error opening file '%s'"),
|
|
|
|
gimp_filename_to_utf8 (filename));
|
2019-08-24 18:56:03 +02:00
|
|
|
return NULL;
|
2003-10-16 13:14:57 +00:00
|
|
|
|
|
|
|
case XpmFileInvalid:
|
2008-08-18 19:08:54 +00:00
|
|
|
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
|
|
|
|
"%s", _("XPM file invalid"));
|
2019-08-24 18:56:03 +02:00
|
|
|
return NULL;
|
2003-10-16 13:14:57 +00:00
|
|
|
|
|
|
|
default:
|
2019-08-24 18:56:03 +02:00
|
|
|
return NULL;
|
2003-10-16 13:14:57 +00:00
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2002-06-29 21:01:33 +00:00
|
|
|
cmap = parse_colors (&xpm_image);
|
2000-01-25 17:46:56 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
image = gimp_image_new (xpm_image.width,
|
|
|
|
xpm_image.height,
|
|
|
|
GIMP_RGB);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
gimp_image_set_filename (image, filename);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
|
|
|
/* fill it */
|
2019-08-24 18:56:03 +02:00
|
|
|
parse_image (image, &xpm_image, cmap);
|
2003-10-16 13:14:57 +00:00
|
|
|
|
|
|
|
g_free (cmap);
|
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
return image;
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2003-10-16 13:14:57 +00:00
|
|
|
static guchar *
|
2019-08-24 18:56:03 +02:00
|
|
|
parse_colors (XpmImage *xpm_image)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2003-07-27 09:46:38 +00:00
|
|
|
#ifndef XPM_NO_X
|
1997-11-24 22:05:25 +00:00
|
|
|
Display *display;
|
|
|
|
Colormap colormap;
|
2003-07-27 09:46:38 +00:00
|
|
|
#endif
|
2000-04-14 08:59:52 +00:00
|
|
|
gint i, j;
|
2002-06-29 21:01:33 +00:00
|
|
|
guchar *cmap;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2003-07-27 09:46:38 +00:00
|
|
|
#ifndef XPM_NO_X
|
1997-11-24 22:05:25 +00:00
|
|
|
/* open the display and get the default color map */
|
|
|
|
display = XOpenDisplay (NULL);
|
2018-03-26 21:25:10 +02:00
|
|
|
if (display == NULL)
|
|
|
|
g_printerr ("Could not open display\n");
|
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
colormap = DefaultColormap (display, DefaultScreen (display));
|
2003-07-27 09:46:38 +00:00
|
|
|
#endif
|
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
/* alloc a buffer to hold the parsed colors */
|
2002-06-29 21:01:33 +00:00
|
|
|
cmap = g_new0 (guchar, 4 * xpm_image->ncolors);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2002-06-29 21:01:33 +00:00
|
|
|
/* parse each color in the file */
|
|
|
|
for (i = 0, j = 0; i < xpm_image->ncolors; i++)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2002-06-29 21:01:33 +00:00
|
|
|
gchar *colorspec = "None";
|
|
|
|
XpmColor *xpm_color;
|
2003-07-27 09:46:38 +00:00
|
|
|
#ifndef XPM_NO_X
|
2002-06-29 21:01:33 +00:00
|
|
|
XColor xcolor;
|
2003-07-27 09:46:38 +00:00
|
|
|
#else
|
|
|
|
GdkColor xcolor;
|
|
|
|
#endif
|
2003-10-16 13:14:57 +00:00
|
|
|
|
2002-06-29 21:01:33 +00:00
|
|
|
xpm_color = &(xpm_image->colorTable[i]);
|
2003-10-16 13:14:57 +00:00
|
|
|
|
2002-06-29 21:01:33 +00:00
|
|
|
/* pick the best spec available */
|
|
|
|
if (xpm_color->c_color)
|
2003-12-10 14:58:00 +00:00
|
|
|
colorspec = xpm_color->c_color;
|
2002-06-29 21:01:33 +00:00
|
|
|
else if (xpm_color->g_color)
|
2003-12-10 14:58:00 +00:00
|
|
|
colorspec = xpm_color->g_color;
|
2002-06-29 21:01:33 +00:00
|
|
|
else if (xpm_color->g4_color)
|
2003-12-10 14:58:00 +00:00
|
|
|
colorspec = xpm_color->g4_color;
|
2002-06-29 21:01:33 +00:00
|
|
|
else if (xpm_color->m_color)
|
2003-12-10 14:58:00 +00:00
|
|
|
colorspec = xpm_color->m_color;
|
2003-10-16 13:14:57 +00:00
|
|
|
|
2004-11-19 20:00:22 +00:00
|
|
|
/* parse if it's not transparent */
|
2002-06-29 21:01:33 +00:00
|
|
|
if (strcmp (colorspec, "None") != 0)
|
2003-12-10 14:58:00 +00:00
|
|
|
{
|
2003-07-27 09:46:38 +00:00
|
|
|
#ifndef XPM_NO_X
|
2003-12-10 14:58:00 +00:00
|
|
|
XParseColor (display, colormap, colorspec, &xcolor);
|
2003-07-27 09:46:38 +00:00
|
|
|
#else
|
2003-12-10 14:58:00 +00:00
|
|
|
gdk_color_parse (colorspec, &xcolor);
|
2003-07-27 09:46:38 +00:00
|
|
|
#endif
|
2003-12-10 14:58:00 +00:00
|
|
|
cmap[j++] = xcolor.red >> 8;
|
|
|
|
cmap[j++] = xcolor.green >> 8;
|
|
|
|
cmap[j++] = xcolor.blue >> 8;
|
|
|
|
cmap[j++] = ~0;
|
|
|
|
}
|
2002-06-29 21:01:33 +00:00
|
|
|
else
|
2003-12-10 14:58:00 +00:00
|
|
|
{
|
|
|
|
j += 4;
|
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
2003-07-27 09:46:38 +00:00
|
|
|
#ifndef XPM_NO_X
|
1997-11-24 22:05:25 +00:00
|
|
|
XCloseDisplay (display);
|
2003-07-27 09:46:38 +00:00
|
|
|
#endif
|
2002-06-29 21:01:33 +00:00
|
|
|
return cmap;
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-08-24 18:56:03 +02:00
|
|
|
parse_image (GimpImage *image,
|
|
|
|
XpmImage *xpm_image,
|
|
|
|
guchar *cmap)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2012-11-26 02:39:44 +01:00
|
|
|
GeglBuffer *buffer;
|
|
|
|
gint tile_height;
|
|
|
|
gint scanlines;
|
|
|
|
gint val;
|
|
|
|
guchar *buf;
|
|
|
|
guchar *dest;
|
|
|
|
guint *src;
|
2019-08-24 18:56:03 +02:00
|
|
|
GimpLayer *layer;
|
2012-11-26 02:39:44 +01:00
|
|
|
gint i;
|
2003-10-16 13:14:57 +00:00
|
|
|
|
2019-08-24 18:56:03 +02: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 13:14:57 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
gimp_image_insert_layer (image, layer, NULL, 0);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
buffer = gimp_drawable_get_buffer (GIMP_DRAWABLE (layer));
|
1997-11-24 22:05:25 +00:00
|
|
|
|
|
|
|
tile_height = gimp_tile_height ();
|
2003-10-16 13:14:57 +00:00
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
buf = g_new (guchar, tile_height * xpm_image->width * 4);
|
|
|
|
|
2002-06-29 21:01:33 +00:00
|
|
|
src = xpm_image->data;
|
2012-11-26 02:39:44 +01:00
|
|
|
for (i = 0; i < xpm_image->height; i += tile_height)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2012-11-26 02:39:44 +01:00
|
|
|
gint j;
|
|
|
|
|
2002-06-29 21:01:33 +00:00
|
|
|
dest = buf;
|
2012-11-26 02:39:44 +01:00
|
|
|
scanlines = MIN (tile_height, xpm_image->height - i);
|
2002-06-29 21:01:33 +00:00
|
|
|
j = scanlines * xpm_image->width;
|
2012-11-26 02:39:44 +01:00
|
|
|
while (j--)
|
2003-12-10 14:58:00 +00:00
|
|
|
{
|
2012-11-26 02:39:44 +01: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-29 21:01:33 +00:00
|
|
|
}
|
2003-10-16 13:14:57 +00:00
|
|
|
|
2012-11-26 02:39:44 +01:00
|
|
|
gegl_buffer_set (buffer,
|
|
|
|
GEGL_RECTANGLE (0, i, xpm_image->width, scanlines), 0,
|
|
|
|
NULL, buf, GEGL_AUTO_ROWSTRIDE);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
2003-10-16 13:14:57 +00:00
|
|
|
|
2012-11-26 02:39:44 +01:00
|
|
|
g_free (buf);
|
|
|
|
g_object_unref (buffer);
|
1998-02-25 06:21:26 +00:00
|
|
|
|
2012-11-26 02:39:44 +01:00
|
|
|
gimp_progress_update (1.0);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2002-06-29 21:01:33 +00:00
|
|
|
static guint
|
2000-01-25 17:46:56 +00:00
|
|
|
rgbhash (rgbkey *c)
|
1998-02-25 06:21:26 +00:00
|
|
|
{
|
1999-10-21 00:11:37 +00:00
|
|
|
return ((guint)c->r) ^ ((guint)c->g) ^ ((guint)c->b);
|
1998-02-25 06:21:26 +00:00
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2002-06-29 21:01:33 +00:00
|
|
|
static guint
|
2003-10-16 13:14:57 +00:00
|
|
|
compare (rgbkey *c1,
|
2003-12-10 14:58:00 +00:00
|
|
|
rgbkey *c2)
|
1998-02-25 06:21:26 +00:00
|
|
|
{
|
2000-04-14 08:59:52 +00:00
|
|
|
return (c1->r == c2->r) && (c1->g == c2->g) && (c1->b == c2->b);
|
1998-02-25 06:21:26 +00:00
|
|
|
}
|
2002-06-29 21:01:33 +00:00
|
|
|
|
|
|
|
static void
|
2003-10-16 13:14:57 +00:00
|
|
|
set_XpmImage (XpmColor *array,
|
2003-12-10 14:58:00 +00:00
|
|
|
guint index,
|
|
|
|
gchar *colorstring)
|
1998-02-25 06:21:26 +00:00
|
|
|
{
|
2000-01-25 17:46:56 +00:00
|
|
|
gchar *p;
|
2012-11-26 02:39:44 +01:00
|
|
|
gint i, charnum, indtemp;
|
2003-10-16 13:14:57 +00:00
|
|
|
|
1999-10-21 00:11:37 +00:00
|
|
|
indtemp=index;
|
2000-04-14 08:59:52 +00:00
|
|
|
array[index].string = p = g_new (gchar, cpp+1);
|
2003-10-16 13:14:57 +00:00
|
|
|
|
1999-10-21 00:11:37 +00:00
|
|
|
/*convert the index number to base sizeof(linenoise)-1 */
|
2019-08-24 18:56:03 +02:00
|
|
|
for (i = 0; i < cpp; ++i)
|
1999-10-21 00:11:37 +00:00
|
|
|
{
|
2000-04-14 08:59:52 +00:00
|
|
|
charnum = indtemp % (sizeof (linenoise) - 1);
|
|
|
|
indtemp = indtemp / (sizeof (linenoise) - 1);
|
|
|
|
*p++ = linenoise[charnum];
|
1999-10-21 00:11:37 +00:00
|
|
|
}
|
|
|
|
/* *p++=linenoise[indtemp]; */
|
2003-10-16 13:14:57 +00:00
|
|
|
|
2000-04-14 08:59:52 +00:00
|
|
|
*p = '\0'; /* C and its stupid null-terminated strings... */
|
1999-10-21 00:11:37 +00:00
|
|
|
|
|
|
|
array[index].symbolic = NULL;
|
2000-04-14 08:59:52 +00:00
|
|
|
array[index].m_color = NULL;
|
1999-10-21 00:11:37 +00:00
|
|
|
array[index].g4_color = NULL;
|
2000-04-14 08:59:52 +00:00
|
|
|
|
1999-10-21 00:11:37 +00:00
|
|
|
if (color)
|
|
|
|
{
|
|
|
|
array[index].g_color = NULL;
|
|
|
|
array[index].c_color = colorstring;
|
2008-09-17 08:37:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-10-21 00:11:37 +00:00
|
|
|
array[index].c_color = NULL;
|
|
|
|
array[index].g_color = colorstring;
|
|
|
|
}
|
1998-02-25 06:21:26 +00:00
|
|
|
}
|
|
|
|
|
2002-06-29 21:01:33 +00:00
|
|
|
static void
|
2003-10-16 13:14:57 +00:00
|
|
|
create_colormap_from_hash (gpointer gkey,
|
2003-12-10 14:58:00 +00:00
|
|
|
gpointer value,
|
|
|
|
gpointer user_data)
|
1998-02-25 06:21:26 +00:00
|
|
|
{
|
2003-03-10 21:07:05 +00:00
|
|
|
rgbkey *key = gkey;
|
|
|
|
gchar *string = g_new (gchar, 8);
|
1999-10-21 00:11:37 +00:00
|
|
|
|
2000-01-25 17:46:56 +00: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 06:21:26 +00:00
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2000-04-14 08:59:52 +00:00
|
|
|
static gboolean
|
2019-08-24 18:56:03 +02:00
|
|
|
save_image (const gchar *filename,
|
|
|
|
GimpImage *image,
|
|
|
|
GimpDrawable *drawable,
|
|
|
|
GError **error)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2012-11-26 02:39:44 +01:00
|
|
|
GeglBuffer *buffer;
|
|
|
|
const Babl *format;
|
|
|
|
gint width;
|
|
|
|
gint height;
|
|
|
|
gint ncolors = 1;
|
|
|
|
gint *indexno;
|
|
|
|
gboolean indexed;
|
|
|
|
gboolean alpha;
|
|
|
|
XpmColor *colormap;
|
2019-08-24 18:56:03 +02:00
|
|
|
XpmImage *xpm_image;
|
2012-11-26 02:39:44 +01:00
|
|
|
guint *ibuff = NULL;
|
|
|
|
guchar *buf;
|
|
|
|
guchar *data;
|
1998-02-25 06:21:26 +00:00
|
|
|
GHashTable *hash = NULL;
|
2012-11-26 02:39:44 +01:00
|
|
|
gint i, j, k;
|
|
|
|
gint threshold = xpmvals.threshold;
|
|
|
|
gboolean success = FALSE;
|
2000-01-25 17:46:56 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
buffer = gimp_drawable_get_buffer (drawable);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2012-11-26 02:39:44 +01:00
|
|
|
width = gegl_buffer_get_width (buffer);
|
|
|
|
height = gegl_buffer_get_height (buffer);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
alpha = gimp_drawable_has_alpha (drawable);
|
|
|
|
color = ! gimp_drawable_is_gray (drawable);
|
|
|
|
indexed = gimp_drawable_is_indexed (drawable);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
switch (gimp_drawable_type (drawable))
|
2012-11-26 02:39:44 +01: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 16:37:48 +05:30
|
|
|
|
|
|
|
default:
|
|
|
|
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
|
|
|
|
_("Unsupported drawable type"));
|
|
|
|
g_object_unref (buffer);
|
|
|
|
return FALSE;
|
2012-11-26 02:39:44 +01:00
|
|
|
}
|
2003-10-16 13:14:57 +00:00
|
|
|
|
2003-03-10 21:07:05 +00:00
|
|
|
/* allocate buffer making the assumption that ibuff is 32 bit aligned... */
|
2004-11-19 20:00:22 +00:00
|
|
|
ibuff = g_new (guint, width * height);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2004-11-19 20:00:22 +00:00
|
|
|
hash = g_hash_table_new ((GHashFunc) rgbhash, (GCompareFunc) compare);
|
2003-10-16 13:14:57 +00:00
|
|
|
|
2016-02-16 02:35:43 +01:00
|
|
|
gimp_progress_init_printf (_("Exporting '%s'"),
|
2005-09-29 18:34:08 +00:00
|
|
|
gimp_filename_to_utf8 (filename));
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2003-03-10 21:07:05 +00:00
|
|
|
ncolors = alpha ? 1 : 0;
|
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
/* allocate a pixel region to work with */
|
2012-11-26 02:39:44 +01:00
|
|
|
buf = g_new (guchar,
|
|
|
|
gimp_tile_height () * width *
|
|
|
|
babl_format_get_bytes_per_pixel (format));
|
1997-11-24 22:05:25 +00:00
|
|
|
|
|
|
|
/* process each row of tiles */
|
2012-11-26 02:39:44 +01:00
|
|
|
for (i = 0; i < height; i += gimp_tile_height ())
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2000-04-14 08:59:52 +00:00
|
|
|
gint scanlines;
|
2002-06-29 21:01:33 +00:00
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
/* read the next row of tiles */
|
2000-04-14 08:59:52 +00:00
|
|
|
scanlines = MIN (gimp_tile_height(), height - i);
|
2012-11-26 02:39:44 +01: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 13:14:57 +00:00
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
/* process each pixel row */
|
2004-11-19 20:00:22 +00:00
|
|
|
for (j = 0; j < scanlines; j++)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
|
|
|
/* go to the start of this row in each image */
|
1998-02-25 06:21:26 +00:00
|
|
|
guint *idata = ibuff + (i+j) * width;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
|
|
|
/* do each pixel in the row */
|
2004-11-19 20:00:22 +00:00
|
|
|
for (k = 0; k < width; k++)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2011-10-12 18:25:17 +05:30
|
|
|
rgbkey *key = g_new (rgbkey, 1);
|
2003-12-10 14:58:00 +00:00
|
|
|
guchar a;
|
2003-10-16 13:14:57 +00:00
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
/* get pixel data */
|
1998-02-25 06:21:26 +00:00
|
|
|
key->r = *(data++);
|
2003-12-10 14:58:00 +00:00
|
|
|
key->g = color && !indexed ? *(data++) : key->r;
|
|
|
|
key->b = color && !indexed ? *(data++) : key->r;
|
|
|
|
a = alpha ? *(data++) : 255;
|
|
|
|
|
|
|
|
if (a < threshold)
|
|
|
|
{
|
|
|
|
*(idata++) = 0;
|
|
|
|
}
|
|
|
|
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 18:25:17 +05:30
|
|
|
key = g_new (rgbkey, 1);
|
2003-12-10 14:58:00 +00:00
|
|
|
}
|
|
|
|
*(idata++) = *indexno;
|
|
|
|
}
|
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
2000-01-25 17:46:56 +00:00
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
/* kick the progress bar */
|
2000-04-14 08:59:52 +00:00
|
|
|
gimp_progress_update ((gdouble) (i+j) / (gdouble) height);
|
2003-10-16 13:14:57 +00:00
|
|
|
}
|
|
|
|
}
|
2008-08-18 19:08:54 +00:00
|
|
|
|
2012-11-26 02:39:44 +01:00
|
|
|
g_free (buf);
|
1998-02-25 06:21:26 +00:00
|
|
|
|
|
|
|
if (indexed)
|
1999-10-03 23:48:33 +00:00
|
|
|
{
|
2019-08-24 18:56:03 +02:00
|
|
|
guchar *cmap = gimp_image_get_colormap (image, &ncolors);
|
2003-03-31 09:34:02 +00:00
|
|
|
guchar *c;
|
|
|
|
|
|
|
|
c = cmap;
|
2003-03-10 21:07:05 +00:00
|
|
|
|
|
|
|
if (alpha)
|
2003-12-10 14:58:00 +00:00
|
|
|
ncolors++;
|
2003-03-10 21:07:05 +00:00
|
|
|
|
2000-04-14 08:59:52 +00:00
|
|
|
colormap = g_new (XpmColor, ncolors);
|
2003-10-16 13:14:57 +00:00
|
|
|
cpp =
|
2003-12-10 14:58:00 +00:00
|
|
|
1 + (gdouble) log (ncolors) / (gdouble) log (sizeof (linenoise) - 1.0);
|
2003-03-10 21:07:05 +00:00
|
|
|
|
|
|
|
if (alpha)
|
2003-12-10 14:58:00 +00:00
|
|
|
set_XpmImage (colormap, 0, "None");
|
2003-03-10 21:07:05 +00:00
|
|
|
|
|
|
|
for (i = alpha ? 1 : 0; i < ncolors; i++)
|
2003-12-10 14:58:00 +00:00
|
|
|
{
|
|
|
|
gchar *string;
|
|
|
|
guchar r, g, b;
|
2003-03-10 21:07:05 +00:00
|
|
|
|
2003-12-10 14:58:00 +00:00
|
|
|
r = *c++;
|
|
|
|
g = *c++;
|
|
|
|
b = *c++;
|
2003-03-10 21:07:05 +00:00
|
|
|
|
2003-12-10 14:58:00 +00:00
|
|
|
string = g_new (gchar, 8);
|
|
|
|
sprintf (string, "#%02X%02X%02X", (int)r, (int)g, (int)b);
|
|
|
|
set_XpmImage (colormap, i, string);
|
|
|
|
}
|
2003-03-10 21:07:05 +00:00
|
|
|
|
|
|
|
g_free (cmap);
|
1999-10-03 23:48:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-04-14 08:59:52 +00:00
|
|
|
colormap = g_new (XpmColor, ncolors);
|
2003-03-10 21:07:05 +00:00
|
|
|
cpp =
|
2003-12-10 14:58:00 +00:00
|
|
|
1 + (gdouble) log (ncolors) / (gdouble) log (sizeof (linenoise) - 1.0);
|
2003-03-10 21:07:05 +00:00
|
|
|
|
|
|
|
if (alpha)
|
2003-12-10 14:58:00 +00:00
|
|
|
set_XpmImage (colormap, 0, "None");
|
2003-03-10 21:07:05 +00:00
|
|
|
|
1999-10-03 23:48:33 +00:00
|
|
|
g_hash_table_foreach (hash, create_colormap_from_hash, colormap);
|
|
|
|
}
|
2000-01-25 17:46:56 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
xpm_image = g_new (XpmImage, 1);
|
2003-10-16 13:14:57 +00:00
|
|
|
|
2019-08-24 18:56:03 +02: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 13:14:57 +00:00
|
|
|
|
1999-10-03 23:48:33 +00:00
|
|
|
/* do the save */
|
2019-08-24 18:56:03 +02:00
|
|
|
switch (XpmWriteFileFromXpmImage ((char *) filename, xpm_image, NULL))
|
2008-08-18 19:08:54 +00:00
|
|
|
{
|
|
|
|
case XpmSuccess:
|
|
|
|
success = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XpmOpenFailed:
|
|
|
|
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
|
|
|
|
_("Error opening file '%s'"),
|
|
|
|
gimp_filename_to_utf8 (filename));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XpmFileInvalid:
|
|
|
|
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
|
|
|
|
"%s", _("XPM file invalid"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2000-01-25 17:46:56 +00:00
|
|
|
|
2012-11-26 02:39:44 +01:00
|
|
|
g_object_unref (buffer);
|
2002-06-29 21:01:33 +00:00
|
|
|
g_free (ibuff);
|
2003-03-10 21:07:05 +00:00
|
|
|
|
2003-10-16 13:14:57 +00:00
|
|
|
if (hash)
|
2000-04-14 08:59:52 +00:00
|
|
|
g_hash_table_destroy (hash);
|
2003-10-16 13:14:57 +00:00
|
|
|
|
2012-11-26 02:39:44 +01:00
|
|
|
gimp_progress_update (1.0);
|
|
|
|
|
2008-08-18 19:08:54 +00:00
|
|
|
return success;
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2004-05-20 01:19:47 +00:00
|
|
|
static gboolean
|
2000-01-11 15:48:00 +00:00
|
|
|
save_dialog (void)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2010-10-25 01:26:00 +02:00
|
|
|
GtkWidget *dialog;
|
2018-05-06 00:35:26 +02:00
|
|
|
GtkWidget *grid;
|
2010-10-25 01:26:00 +02:00
|
|
|
GtkAdjustment *scale_data;
|
|
|
|
gboolean run;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2009-07-21 14:10:29 +02:00
|
|
|
dialog = gimp_export_dialog_new (_("XPM"), PLUG_IN_BINARY, SAVE_PROC);
|
2005-09-09 18:38:00 +00:00
|
|
|
|
2018-05-06 00:35:26 +02:00
|
|
|
grid = gtk_grid_new ();
|
|
|
|
gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
|
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (grid), 12);
|
2009-07-21 14:10:29 +02:00
|
|
|
gtk_box_pack_start (GTK_BOX (gimp_export_dialog_get_content_area (dialog)),
|
2018-05-06 00:35:26 +02:00
|
|
|
grid, TRUE, TRUE, 0);
|
|
|
|
gtk_widget_show (grid);
|
|
|
|
|
2018-05-11 13:52:33 +02:00
|
|
|
scale_data = gimp_scale_entry_new (GTK_GRID (grid), 0, 0,
|
|
|
|
_("_Alpha threshold:"), SCALE_WIDTH, 0,
|
|
|
|
xpmvals.threshold, 0, 255, 1, 8, 0,
|
|
|
|
TRUE, 0, 0,
|
|
|
|
NULL, NULL);
|
2000-04-14 08:59:52 +00:00
|
|
|
|
2005-06-30 16:03:24 +00:00
|
|
|
g_signal_connect (scale_data, "value-changed",
|
2002-01-15 18:35:29 +00:00
|
|
|
G_CALLBACK (gimp_int_adjustment_update),
|
|
|
|
&xpmvals.threshold);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2005-09-09 18:38:00 +00:00
|
|
|
gtk_widget_show (dialog);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2005-09-09 18:38:00 +00:00
|
|
|
run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2005-09-09 18:38:00 +00:00
|
|
|
gtk_widget_destroy (dialog);
|
2000-01-11 15:48:00 +00:00
|
|
|
|
2003-11-06 15:27:05 +00:00
|
|
|
return run;
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|