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
|
|
|
*/
|
|
|
|
|
2020-02-01 17:00:07 +00:00
|
|
|
/* XPM plugin version 1.2.7 */
|
1998-03-17 00:31:58 +00:00
|
|
|
|
2000-04-14 08:59:52 +00:00
|
|
|
/*
|
2020-02-01 17:00:07 +00:00
|
|
|
1.2.7 fixes saving unused transparency (bug #4560)
|
|
|
|
|
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"
|
2024-04-13 15:10:25 +00:00
|
|
|
#define EXPORT_PROC "file-xpm-export"
|
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
|
|
|
|
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 ())
|
2023-10-18 18:29:37 +02:00
|
|
|
#define XPM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), XPM_TYPE, Xpm))
|
2019-08-24 18:56:03 +02:00
|
|
|
|
|
|
|
GType xpm_get_type (void) G_GNUC_CONST;
|
|
|
|
|
2023-08-06 02:56:44 +02:00
|
|
|
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,
|
|
|
|
GimpMetadata *metadata,
|
|
|
|
GimpMetadataLoadFlags *flags,
|
|
|
|
GimpProcedureConfig *config,
|
|
|
|
gpointer run_data);
|
2024-04-13 15:10:25 +00:00
|
|
|
static GimpValueArray * xpm_export (GimpProcedure *procedure,
|
2023-08-06 02:56:44 +02:00
|
|
|
GimpRunMode run_mode,
|
|
|
|
GimpImage *image,
|
|
|
|
GFile *file,
|
2024-05-06 18:38:12 +00:00
|
|
|
GimpExportOptions *options,
|
2023-08-06 02:56:44 +02:00
|
|
|
GimpMetadata *metadata,
|
|
|
|
GimpProcedureConfig *config,
|
|
|
|
gpointer run_data);
|
|
|
|
|
|
|
|
static GimpImage * load_image (GFile *file,
|
|
|
|
GError **error);
|
|
|
|
static guchar * parse_colors (XpmImage *xpm_image);
|
|
|
|
static void parse_image (GimpImage *image,
|
|
|
|
XpmImage *xpm_image,
|
|
|
|
guchar *cmap);
|
2024-04-13 15:10:25 +00:00
|
|
|
static gboolean export_image (GFile *file,
|
2023-08-06 02:56:44 +02:00
|
|
|
GimpImage *image,
|
|
|
|
GimpDrawable *drawable,
|
|
|
|
GObject *config,
|
|
|
|
GError **error);
|
|
|
|
static gboolean save_dialog (GimpImage *image,
|
|
|
|
GimpProcedure *procedure,
|
|
|
|
GObject *config);
|
2019-08-24 18:56:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (Xpm, xpm, GIMP_TYPE_PLUG_IN)
|
|
|
|
|
|
|
|
GIMP_MAIN (XPM_TYPE)
|
2022-05-26 00:59:36 +02:00
|
|
|
DEFINE_STD_SET_I18N
|
2019-08-24 18:56:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
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;
|
2022-05-26 00:59:36 +02:00
|
|
|
plug_in_class->set_i18n = STD_SET_I18N;
|
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));
|
2024-04-13 15:10:25 +00:00
|
|
|
list = g_list_append (list, g_strdup (EXPORT_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
|
|
|
{
|
2023-08-06 03:21:27 +02:00
|
|
|
procedure = gimp_load_procedure_new (plug_in, name,
|
|
|
|
GIMP_PDB_PROC_TYPE_PLUGIN,
|
|
|
|
xpm_load, NULL, NULL);
|
2019-08-24 18:56:03 +02:00
|
|
|
|
2022-07-04 22:50:53 +02:00
|
|
|
gimp_procedure_set_menu_label (procedure, _("X PixMap image"));
|
2019-08-24 18:56:03 +02:00
|
|
|
|
|
|
|
gimp_procedure_set_documentation (procedure,
|
2024-05-06 18:38:12 +00:00
|
|
|
_("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."),
|
2019-08-24 18:56:03 +02:00
|
|
|
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
|
|
|
}
|
2024-04-13 15:10:25 +00:00
|
|
|
else if (! strcmp (name, EXPORT_PROC))
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2024-04-20 03:08:57 +00:00
|
|
|
procedure = gimp_export_procedure_new (plug_in, name,
|
|
|
|
GIMP_PDB_PROC_TYPE_PLUGIN,
|
|
|
|
FALSE, xpm_export, NULL, NULL);
|
2019-08-24 18:56:03 +02:00
|
|
|
|
|
|
|
gimp_procedure_set_image_types (procedure, "*");
|
|
|
|
|
2022-07-04 22:50:53 +02:00
|
|
|
gimp_procedure_set_menu_label (procedure, _("X PixMap image"));
|
2019-08-24 18:56:03 +02:00
|
|
|
|
|
|
|
gimp_procedure_set_documentation (procedure,
|
2024-05-06 18:38:12 +00:00
|
|
|
_("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."),
|
2019-08-24 18:56:03 +02:00
|
|
|
name);
|
|
|
|
gimp_procedure_set_attribution (procedure,
|
|
|
|
"Spencer Kimball & Peter Mattis & "
|
|
|
|
"Ray Lehtiniemi & Nathan Summers",
|
|
|
|
"Spencer Kimball & Peter Mattis",
|
|
|
|
"1997");
|
|
|
|
|
2023-05-20 14:30:41 +00:00
|
|
|
gimp_file_procedure_set_format_name (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
_("XPM"));
|
2019-08-24 18:56:03 +02:00
|
|
|
gimp_file_procedure_set_mime_types (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"image/x-pixmap");
|
|
|
|
gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"xpm");
|
|
|
|
|
2024-05-06 18:38:12 +00:00
|
|
|
gimp_export_procedure_set_capabilities (GIMP_EXPORT_PROCEDURE (procedure),
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_RGB |
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_GRAY |
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_INDEXED |
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_ALPHA,
|
app, libgimp*, pdb, plug-ins: review and enhance MR !1549.
- Fix annotations for gimp_export_options_get_image() to make it
actually introspectable with the GimpImage being both input and
output. Even though the logic doesn't change much (the input image may
be overriden or not), it doesn't matter for introspection because
images are handled centrally by libgimp and therefore must not be
freed. Actually deleting the image from the central list of images
though remains a manual action depending on code logic, not some
automatic action to be handled by binding engines.
- Add G_GNUC_WARN_UNUSED_RESULT to gimp_export_options_get_image()
because ignoring the returned value is rarely a good idea (as you
usually want to delete the image).
- Remove gimp_export_options_new(): we don't need this constructor
because at this point, the best is to tell plug-in developers to just
pass NULL everywhere. This leaves us free to create a more useful
default constructor if needed, in the future. Main description for
GimpExportOptions has also been updated to say this.
- Add a data_destroy callback for the user data passed in
gimp_export_procedure_set_capabilities().
- Fixing annotations of 'export_options' object from pdb/pdb.pl: input
args would actually be (nullable) and would not transfer ownership
(calling code must still free the object). Return value's ownership on
the other hand is fully transfered.
- Add C and Python unit testing for GimpExportOptions and
gimp_export_options_get_image() in particular.
- Fix or improve various details.
Note that I have also considered for a long time changing the signature
of gimp_export_options_get_image() to return a boolean indicating
whether `image` had been replaced (hence needed deletion) or not. This
also meant getting rid of the GimpExportReturn enum. Right now it would
work because there are no third case, but I was considering the future
possibility that for instance we got some impossible conversion for some
future capability. I'm not sure it would ever happen; and for sure, this
is not desirable because it implies an export failure a bit late in the
workflow. But just in case, let's keep the enum return value. It does
not even make the using code that much more complicated (well just a
value comparison instead of a simple boolean test).
2024-08-17 15:06:27 +02:00
|
|
|
NULL, NULL, NULL);
|
2024-05-06 18:38:12 +00:00
|
|
|
|
2024-06-12 16:53:12 +00:00
|
|
|
gimp_procedure_add_int_argument (procedure, "threshold",
|
|
|
|
_("_Threshold"),
|
|
|
|
_("Alpha threshold"),
|
|
|
|
0, 255, 127,
|
|
|
|
G_PARAM_READWRITE);
|
2019-08-24 18:56:03 +02:00
|
|
|
}
|
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 *
|
2023-08-06 02:56:44 +02:00
|
|
|
xpm_load (GimpProcedure *procedure,
|
|
|
|
GimpRunMode run_mode,
|
|
|
|
GFile *file,
|
|
|
|
GimpMetadata *metadata,
|
|
|
|
GimpMetadataLoadFlags *flags,
|
|
|
|
GimpProcedureConfig *config,
|
|
|
|
gpointer run_data)
|
2019-08-24 18:56:03 +02:00
|
|
|
{
|
|
|
|
GimpValueArray *return_vals;
|
|
|
|
GimpImage *image;
|
|
|
|
GError *error = NULL;
|
2000-01-25 17:46:56 +00:00
|
|
|
|
2019-08-24 18:56:03 +02:00
|
|
|
gegl_init (NULL, NULL);
|
1999-10-03 23:48:33 +00:00
|
|
|
|
2019-09-11 21:48:34 +02:00
|
|
|
image = load_image (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 *
|
2024-04-13 15:10:25 +00:00
|
|
|
xpm_export (GimpProcedure *procedure,
|
|
|
|
GimpRunMode run_mode,
|
|
|
|
GimpImage *image,
|
|
|
|
GFile *file,
|
2024-05-06 18:38:12 +00:00
|
|
|
GimpExportOptions *options,
|
2024-04-13 15:10:25 +00:00
|
|
|
GimpMetadata *metadata,
|
|
|
|
GimpProcedureConfig *config,
|
|
|
|
gpointer run_data)
|
2019-08-24 18:56:03 +02:00
|
|
|
{
|
2023-07-21 18:08:00 +02:00
|
|
|
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
|
2024-04-30 04:25:51 +00:00
|
|
|
GimpExportReturn export = GIMP_EXPORT_IGNORE;
|
2024-04-30 13:50:24 +00:00
|
|
|
GList *drawables;
|
|
|
|
GError *error = NULL;
|
2019-08-24 18:56:03 +02:00
|
|
|
|
|
|
|
gegl_init (NULL, NULL);
|
|
|
|
|
2024-05-06 18:38:12 +00:00
|
|
|
export = gimp_export_options_get_image (options, &image);
|
2024-04-30 13:50:24 +00:00
|
|
|
drawables = gimp_image_list_layers (image);
|
2020-04-14 11:46:17 +02:00
|
|
|
|
2019-09-26 12:09:41 +02:00
|
|
|
if (run_mode == GIMP_RUN_INTERACTIVE)
|
2000-01-25 17:46:56 +00:00
|
|
|
{
|
2024-07-14 20:12:57 +00:00
|
|
|
gimp_ui_init (PLUG_IN_BINARY);
|
2024-05-06 18:38:12 +00:00
|
|
|
|
2024-04-30 13:50:24 +00:00
|
|
|
if (gimp_drawable_has_alpha (drawables->data))
|
2023-05-20 14:30:41 +00:00
|
|
|
if (! save_dialog (image, procedure, G_OBJECT (config)))
|
2019-08-24 18:56:03 +02:00
|
|
|
status = GIMP_PDB_CANCEL;
|
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
|
|
|
{
|
2024-04-30 13:50:24 +00:00
|
|
|
if (! export_image (file, image, drawables->data, G_OBJECT (config),
|
2024-04-13 15:10:25 +00:00
|
|
|
&error))
|
2019-08-24 18:56:03 +02:00
|
|
|
{
|
|
|
|
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)
|
2024-04-30 13:50:24 +00:00
|
|
|
gimp_image_delete (image);
|
2019-08-24 18:56:03 +02:00
|
|
|
|
2024-04-30 13:50:24 +00:00
|
|
|
g_list_free (drawables);
|
2019-08-24 18:56:03 +02:00
|
|
|
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 *
|
2019-09-11 21:48:34 +02:00
|
|
|
load_image (GFile *file,
|
|
|
|
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'"),
|
2019-09-11 21:48:34 +02:00
|
|
|
gimp_file_get_utf8_name (file));
|
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
/* read the raw file */
|
2024-01-14 14:30:32 +00:00
|
|
|
switch (XpmReadFileToXpmImage ((char *) g_file_peek_path (file),
|
|
|
|
&xpm_image, NULL))
|
2003-10-16 13:14:57 +00:00
|
|
|
{
|
|
|
|
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'"),
|
2019-09-11 21:48:34 +02:00
|
|
|
gimp_file_get_utf8_name (file));
|
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
|
|
|
|
|
|
|
/* 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";
|
2023-05-20 15:17:14 +00:00
|
|
|
XpmColor *xpm_color;
|
2003-07-27 09:46:38 +00:00
|
|
|
#ifndef XPM_NO_X
|
2023-05-20 15:17:14 +00:00
|
|
|
XColor xcolor;
|
2003-07-27 09:46:38 +00:00
|
|
|
#else
|
2023-05-20 15:17:14 +00:00
|
|
|
GdkRGBA xcolor;
|
2003-07-27 09:46:38 +00:00
|
|
|
#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);
|
|
|
|
cmap[j++] = xcolor.red >> 8;
|
|
|
|
cmap[j++] = xcolor.green >> 8;
|
|
|
|
cmap[j++] = xcolor.blue >> 8;
|
2023-05-20 15:17:14 +00:00
|
|
|
#else
|
|
|
|
gdk_rgba_parse (&xcolor, colorspec);
|
|
|
|
cmap[j++] = CLAMP (xcolor.red * G_MAXUINT8, 0, G_MAXUINT8);
|
|
|
|
cmap[j++] = CLAMP (xcolor.green * G_MAXUINT8, 0, G_MAXUINT8);
|
|
|
|
cmap[j++] = CLAMP (xcolor.blue * G_MAXUINT8, 0, G_MAXUINT8);
|
|
|
|
#endif
|
2003-12-10 14:58:00 +00:00
|
|
|
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
|
|
|
|
2020-02-01 17:00:07 +00:00
|
|
|
static void
|
|
|
|
decrement_hash_values (gpointer gkey,
|
|
|
|
gpointer value,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
--(*((guint*) value));
|
|
|
|
}
|
|
|
|
|
2000-04-14 08:59:52 +00:00
|
|
|
static gboolean
|
2024-04-13 15:10:25 +00:00
|
|
|
export_image (GFile *file,
|
|
|
|
GimpImage *image,
|
|
|
|
GimpDrawable *drawable,
|
|
|
|
GObject *config,
|
|
|
|
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;
|
2020-02-01 17:00:07 +00:00
|
|
|
gboolean alpha_used = FALSE;
|
2012-11-26 02:39:44 +01:00
|
|
|
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;
|
2019-09-26 12:09:41 +02:00
|
|
|
gint threshold;
|
2012-11-26 02:39:44 +01:00
|
|
|
gboolean success = FALSE;
|
2000-01-25 17:46:56 +00:00
|
|
|
|
2019-09-26 12:09:41 +02:00
|
|
|
g_object_get (config,
|
|
|
|
"threshold", &threshold,
|
|
|
|
NULL);
|
|
|
|
|
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'"),
|
2019-09-11 21:48:34 +02:00
|
|
|
gimp_file_get_utf8_name (file));
|
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;
|
2020-02-01 17:00:07 +00:00
|
|
|
alpha_used = TRUE;
|
2003-12-10 14:58:00 +00: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 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
|
|
|
|
2020-02-01 17:00:07 +00: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 06:21:26 +00:00
|
|
|
if (indexed)
|
1999-10-03 23:48:33 +00:00
|
|
|
{
|
2024-09-23 15:17:04 +02:00
|
|
|
guchar *cmap = gimp_palette_get_colormap (gimp_image_get_palette (image), babl_format ("R'G'B' u8"), &ncolors, NULL);
|
2003-03-31 09:34:02 +00:00
|
|
|
guchar *c;
|
|
|
|
|
|
|
|
c = cmap;
|
2003-03-10 21:07:05 +00:00
|
|
|
|
2020-02-01 17:00:07 +00:00
|
|
|
if (alpha_used)
|
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
|
|
|
|
2020-02-01 17:00:07 +00:00
|
|
|
if (alpha_used)
|
2003-12-10 14:58:00 +00:00
|
|
|
set_XpmImage (colormap, 0, "None");
|
2003-03-10 21:07:05 +00:00
|
|
|
|
2020-02-01 17:00:07 +00:00
|
|
|
for (i = alpha_used ? 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
|
|
|
|
2020-02-01 17:00:07 +00:00
|
|
|
if (alpha_used)
|
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
|
|
|
|
2024-04-13 15:10:25 +00:00
|
|
|
/* do the export */
|
2024-01-14 14:30:32 +00:00
|
|
|
switch (XpmWriteFileFromXpmImage ((char *) g_file_peek_path (file),
|
|
|
|
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'"),
|
2019-09-11 21:48:34 +02:00
|
|
|
gimp_file_get_utf8_name (file));
|
2008-08-18 19:08:54 +00:00
|
|
|
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
|
2023-05-20 14:30:41 +00:00
|
|
|
save_dialog (GimpImage *image,
|
|
|
|
GimpProcedure *procedure,
|
2019-09-26 12:09:41 +02:00
|
|
|
GObject *config)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2019-09-26 12:09:41 +02:00
|
|
|
GtkWidget *dialog;
|
|
|
|
gboolean run;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2024-04-20 03:08:57 +00:00
|
|
|
dialog = gimp_export_procedure_dialog_new (GIMP_EXPORT_PROCEDURE (procedure),
|
|
|
|
GIMP_PROCEDURE_CONFIG (config),
|
|
|
|
image);
|
2005-09-09 18:38:00 +00:00
|
|
|
|
2023-03-26 02:19:07 +00:00
|
|
|
gimp_procedure_dialog_get_scale_entry (GIMP_PROCEDURE_DIALOG (dialog),
|
|
|
|
"threshold", 1.0);
|
|
|
|
|
|
|
|
gimp_procedure_dialog_fill (GIMP_PROCEDURE_DIALOG (dialog), NULL);
|
2005-09-09 18:38:00 +00:00
|
|
|
gtk_widget_show (dialog);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-09-26 12:09:41 +02:00
|
|
|
run = gimp_procedure_dialog_run (GIMP_PROCEDURE_DIALOG (dialog));
|
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
|
|
|
}
|