1999-07-07 01:26:06 +08:00
|
|
|
/* CSource - GIMP Plugin to dump image data in RGB(A) format for C source
|
|
|
|
* Copyright (C) 1999 Tim Janik
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or
|
1999-07-07 01:26:06 +08:00
|
|
|
* modify it under the terms of the GNU General Public License
|
2009-01-18 06:28:01 +08:00
|
|
|
* as published by the Free Software Foundation; either version 3
|
1999-07-07 01:26:06 +08:00
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* 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/>.
|
1999-07-07 01:26:06 +08:00
|
|
|
*
|
|
|
|
* This plugin is heavily based on the header plugin by Spencer Kimball and
|
|
|
|
* Peter Mattis.
|
|
|
|
*/
|
2000-01-26 01:46:56 +08:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
1999-07-07 01:26:06 +08:00
|
|
|
#include <string.h>
|
2008-08-18 04:56:01 +08:00
|
|
|
#include <errno.h>
|
2000-01-26 01:46:56 +08:00
|
|
|
|
2005-03-04 23:12:29 +08:00
|
|
|
#include <glib/gstdio.h>
|
2000-01-26 01:46:56 +08:00
|
|
|
|
|
|
|
#include <libgimp/gimp.h>
|
|
|
|
#include <libgimp/gimpui.h>
|
|
|
|
|
2000-01-17 21:21:13 +08:00
|
|
|
#include "libgimp/stdplugins-intl.h"
|
1999-07-07 01:26:06 +08:00
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
|
2024-04-13 23:10:25 +08:00
|
|
|
#define EXPORT_PROC "file-csource-export"
|
2008-08-11 18:06:13 +08:00
|
|
|
#define PLUG_IN_BINARY "file-csource"
|
2011-04-09 02:31:34 +08:00
|
|
|
#define PLUG_IN_ROLE "gimp-file-csource"
|
2005-08-14 02:29:14 +08:00
|
|
|
|
|
|
|
|
2019-08-24 19:33:54 +08:00
|
|
|
typedef struct _Csource Csource;
|
|
|
|
typedef struct _CsourceClass CsourceClass;
|
|
|
|
|
|
|
|
struct _Csource
|
|
|
|
{
|
|
|
|
GimpPlugIn parent_instance;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _CsourceClass
|
1999-07-07 01:26:06 +08:00
|
|
|
{
|
2019-08-24 19:33:54 +08:00
|
|
|
GimpPlugInClass parent_class;
|
2000-01-26 01:46:56 +08:00
|
|
|
};
|
|
|
|
|
2019-08-24 19:33:54 +08:00
|
|
|
|
|
|
|
#define CSOURCE_TYPE (csource_get_type ())
|
2023-10-19 00:29:37 +08:00
|
|
|
#define CSOURCE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CSOURCE_TYPE, Csource))
|
2019-08-24 19:33:54 +08:00
|
|
|
|
|
|
|
GType csource_get_type (void) G_GNUC_CONST;
|
|
|
|
|
|
|
|
static GList * csource_query_procedures (GimpPlugIn *plug_in);
|
|
|
|
static GimpProcedure * csource_create_procedure (GimpPlugIn *plug_in,
|
|
|
|
const gchar *name);
|
|
|
|
|
2024-04-13 23:10:25 +08:00
|
|
|
static GimpValueArray * csource_export (GimpProcedure *procedure,
|
2019-08-24 19:33:54 +08:00
|
|
|
GimpRunMode run_mode,
|
|
|
|
GimpImage *image,
|
|
|
|
GFile *file,
|
2024-05-07 02:38:12 +08:00
|
|
|
GimpExportOptions *options,
|
2023-07-21 05:16:27 +08:00
|
|
|
GimpMetadata *metadata,
|
|
|
|
GimpProcedureConfig *config,
|
2019-08-24 19:33:54 +08:00
|
|
|
gpointer run_data);
|
|
|
|
|
2024-04-13 23:10:25 +08:00
|
|
|
static gboolean export_image (GFile *file,
|
2019-08-24 19:33:54 +08:00
|
|
|
GimpImage *image,
|
|
|
|
GimpDrawable *drawable,
|
2019-10-06 01:29:20 +08:00
|
|
|
GObject *config,
|
2019-08-24 19:33:54 +08:00
|
|
|
GError **error);
|
2023-05-20 22:30:41 +08:00
|
|
|
static gboolean save_dialog (GimpImage *image,
|
|
|
|
GimpProcedure *procedure,
|
2019-10-06 01:29:20 +08:00
|
|
|
GObject *config);
|
2019-08-24 19:33:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (Csource, csource, GIMP_TYPE_PLUG_IN)
|
|
|
|
|
|
|
|
GIMP_MAIN (CSOURCE_TYPE)
|
2022-05-26 06:59:36 +08:00
|
|
|
DEFINE_STD_SET_I18N
|
2019-08-24 19:33:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
csource_class_init (CsourceClass *klass)
|
|
|
|
{
|
|
|
|
GimpPlugInClass *plug_in_class = GIMP_PLUG_IN_CLASS (klass);
|
1999-07-07 01:26:06 +08:00
|
|
|
|
2019-08-24 19:33:54 +08:00
|
|
|
plug_in_class->query_procedures = csource_query_procedures;
|
|
|
|
plug_in_class->create_procedure = csource_create_procedure;
|
2022-05-26 06:59:36 +08:00
|
|
|
plug_in_class->set_i18n = STD_SET_I18N;
|
2019-08-24 19:33:54 +08:00
|
|
|
}
|
2012-11-19 07:25:10 +08:00
|
|
|
|
1999-07-07 01:26:06 +08:00
|
|
|
static void
|
2019-08-24 19:33:54 +08:00
|
|
|
csource_init (Csource *csource)
|
1999-07-07 01:26:06 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-08-24 19:33:54 +08:00
|
|
|
static GList *
|
|
|
|
csource_query_procedures (GimpPlugIn *plug_in)
|
1999-07-07 01:26:06 +08:00
|
|
|
{
|
2024-04-13 23:10:25 +08:00
|
|
|
return g_list_append (NULL, g_strdup (EXPORT_PROC));
|
2019-08-24 19:33:54 +08:00
|
|
|
}
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2019-08-24 19:33:54 +08:00
|
|
|
static GimpProcedure *
|
|
|
|
csource_create_procedure (GimpPlugIn *plug_in,
|
|
|
|
const gchar *name)
|
|
|
|
{
|
|
|
|
GimpProcedure *procedure = NULL;
|
|
|
|
|
2024-04-13 23:10:25 +08:00
|
|
|
if (! strcmp (name, EXPORT_PROC))
|
2019-08-24 19:33:54 +08:00
|
|
|
{
|
2024-04-20 11:08:57 +08:00
|
|
|
procedure = gimp_export_procedure_new (plug_in, name,
|
|
|
|
GIMP_PDB_PROC_TYPE_PLUGIN,
|
|
|
|
FALSE, csource_export, NULL, NULL);
|
2019-08-24 19:33:54 +08:00
|
|
|
|
|
|
|
gimp_procedure_set_image_types (procedure, "*");
|
|
|
|
|
2022-07-05 04:50:53 +08:00
|
|
|
gimp_procedure_set_menu_label (procedure, _("C source code"));
|
2019-08-24 19:33:54 +08:00
|
|
|
|
|
|
|
gimp_procedure_set_documentation (procedure,
|
2023-03-30 10:04:00 +08:00
|
|
|
_("Dump image data in RGB(A) format "
|
|
|
|
"for C source"),
|
|
|
|
_("CSource cannot be run non-interactively."),
|
2019-08-24 19:33:54 +08:00
|
|
|
name);
|
|
|
|
gimp_procedure_set_attribution (procedure,
|
|
|
|
"Tim Janik",
|
|
|
|
"Tim Janik",
|
|
|
|
"1999");
|
|
|
|
|
2023-05-20 22:30:41 +08:00
|
|
|
gimp_file_procedure_set_format_name (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
_("C-Source"));
|
2019-08-24 19:33:54 +08:00
|
|
|
gimp_file_procedure_set_handles_remote (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
TRUE);
|
|
|
|
gimp_file_procedure_set_mime_types (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"image/x-csrc");
|
|
|
|
gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"c");
|
2019-10-06 01:29:20 +08:00
|
|
|
|
2024-05-07 02:38:12 +08:00
|
|
|
gimp_export_procedure_set_capabilities (GIMP_EXPORT_PROCEDURE (procedure),
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_RGB |
|
|
|
|
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 21:06:27 +08:00
|
|
|
NULL, NULL, NULL);
|
2024-05-07 02:38:12 +08:00
|
|
|
|
2024-06-13 00:53:12 +08:00
|
|
|
gimp_procedure_add_string_aux_argument (procedure, "prefixed-name",
|
|
|
|
_("_Prefixed name"),
|
|
|
|
_("Prefixed name"),
|
|
|
|
"gimp_image",
|
|
|
|
GIMP_PARAM_READWRITE);
|
|
|
|
|
|
|
|
gimp_procedure_add_string_aux_argument (procedure, "gimp-comment",
|
|
|
|
_("Comme_nt"),
|
|
|
|
_("Comment"),
|
|
|
|
gimp_get_default_comment (),
|
|
|
|
GIMP_PARAM_READWRITE);
|
2019-10-06 01:29:20 +08:00
|
|
|
|
2020-06-16 05:54:09 +08:00
|
|
|
gimp_procedure_set_argument_sync (procedure, "gimp-comment",
|
|
|
|
GIMP_ARGUMENT_SYNC_PARASITE);
|
|
|
|
|
2024-06-13 00:53:12 +08:00
|
|
|
gimp_procedure_add_boolean_aux_argument (procedure, "save-comment",
|
|
|
|
_("Save comment to _file"),
|
|
|
|
_("Save comment"),
|
|
|
|
gimp_export_comment (),
|
|
|
|
GIMP_PARAM_READWRITE);
|
|
|
|
|
|
|
|
gimp_procedure_add_boolean_aux_argument (procedure, "glib-types",
|
|
|
|
_("Use GLib types (guint_8*)"),
|
|
|
|
_("Use GLib types"),
|
|
|
|
TRUE,
|
|
|
|
GIMP_PARAM_READWRITE);
|
|
|
|
|
|
|
|
gimp_procedure_add_boolean_aux_argument (procedure, "save-alpha",
|
|
|
|
_("Save alpha channel (RG_BA/RGB)"),
|
|
|
|
_("Save the alpha channel"),
|
|
|
|
FALSE,
|
|
|
|
GIMP_PARAM_READWRITE);
|
|
|
|
|
|
|
|
gimp_procedure_add_boolean_aux_argument (procedure, "rgb565",
|
|
|
|
_("Save as RGB565 (1_6-bit)"),
|
|
|
|
_("Use RGB565 encoding"),
|
|
|
|
FALSE,
|
|
|
|
GIMP_PARAM_READWRITE);
|
|
|
|
|
|
|
|
gimp_procedure_add_boolean_aux_argument (procedure, "use-macros",
|
|
|
|
_("_Use macros instead of struct"),
|
|
|
|
_("Use C macros"),
|
|
|
|
FALSE,
|
|
|
|
GIMP_PARAM_READWRITE);
|
|
|
|
|
|
|
|
gimp_procedure_add_boolean_aux_argument (procedure, "use-rle",
|
|
|
|
_("Use _1 bit Run-Length-Encoding"),
|
|
|
|
_("Use run-length-encoding"),
|
|
|
|
FALSE,
|
|
|
|
GIMP_PARAM_READWRITE);
|
|
|
|
|
|
|
|
gimp_procedure_add_double_aux_argument (procedure, "opacity",
|
|
|
|
_("Opaci_ty"),
|
|
|
|
_("Opacity"),
|
|
|
|
0.0, 100.0, 100.0,
|
|
|
|
GIMP_PARAM_READWRITE);
|
2019-08-24 19:33:54 +08:00
|
|
|
}
|
2012-11-19 07:25:10 +08:00
|
|
|
|
2019-08-24 19:33:54 +08:00
|
|
|
return procedure;
|
|
|
|
}
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2019-08-24 19:33:54 +08:00
|
|
|
static GimpValueArray *
|
2024-04-13 23:10:25 +08:00
|
|
|
csource_export (GimpProcedure *procedure,
|
|
|
|
GimpRunMode run_mode,
|
|
|
|
GimpImage *image,
|
|
|
|
GFile *file,
|
2024-05-07 02:38:12 +08:00
|
|
|
GimpExportOptions *options,
|
2024-04-13 23:10:25 +08:00
|
|
|
GimpMetadata *metadata,
|
|
|
|
GimpProcedureConfig *config,
|
|
|
|
gpointer run_data)
|
2019-08-24 19:33:54 +08:00
|
|
|
{
|
2023-07-21 05:16:27 +08:00
|
|
|
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
|
2024-04-30 12:25:51 +08:00
|
|
|
GimpExportReturn export = GIMP_EXPORT_IGNORE;
|
2024-04-30 21:50:24 +08:00
|
|
|
GList *drawables;
|
2023-07-21 05:16:27 +08:00
|
|
|
gchar *prefixed_name;
|
|
|
|
gchar *comment;
|
|
|
|
GError *error = NULL;
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2019-08-24 19:33:54 +08:00
|
|
|
gegl_init (NULL, NULL);
|
2000-01-17 21:21:13 +08:00
|
|
|
|
2019-08-24 19:33:54 +08:00
|
|
|
if (run_mode != GIMP_RUN_INTERACTIVE)
|
|
|
|
return gimp_procedure_new_return_values (procedure,
|
|
|
|
GIMP_PDB_CALLING_ERROR,
|
|
|
|
NULL);
|
2000-01-26 01:46:56 +08:00
|
|
|
|
2019-09-21 01:39:00 +08:00
|
|
|
gimp_ui_init (PLUG_IN_BINARY);
|
2008-08-18 04:56:01 +08:00
|
|
|
|
2024-05-07 02:38:12 +08:00
|
|
|
export = gimp_export_options_get_image (options, &image);
|
2024-04-30 21:50:24 +08:00
|
|
|
drawables = gimp_image_list_layers (image);
|
2020-04-14 17:46:17 +08:00
|
|
|
|
|
|
|
g_object_set (config,
|
2024-04-30 21:50:24 +08:00
|
|
|
"save-alpha", gimp_drawable_has_alpha (drawables->data),
|
2020-04-14 17:46:17 +08:00
|
|
|
NULL);
|
|
|
|
|
2023-05-20 22:30:41 +08:00
|
|
|
if (! save_dialog (image, procedure, G_OBJECT (config)))
|
2019-10-06 01:29:20 +08:00
|
|
|
status = GIMP_PDB_CANCEL;
|
|
|
|
|
|
|
|
g_object_get (config,
|
|
|
|
"prefixed-name", &prefixed_name,
|
2020-06-16 05:54:09 +08:00
|
|
|
"gimp-comment", &comment,
|
2019-10-06 01:29:20 +08:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (! prefixed_name || ! prefixed_name[0])
|
|
|
|
g_object_set (config,
|
|
|
|
"prefixed-name", "tmp",
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (comment && ! comment[0])
|
|
|
|
g_object_set (config,
|
2020-06-16 05:54:09 +08:00
|
|
|
"gimp-comment", NULL,
|
2019-10-06 01:29:20 +08:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
g_free (prefixed_name);
|
|
|
|
g_free (comment);
|
|
|
|
|
|
|
|
if (status == GIMP_PDB_SUCCESS)
|
2019-08-24 19:33:54 +08:00
|
|
|
{
|
2024-04-30 21:50:24 +08:00
|
|
|
if (! export_image (file, image, drawables->data, G_OBJECT (config),
|
2024-04-13 23:10:25 +08:00
|
|
|
&error))
|
2008-08-18 04:56:01 +08:00
|
|
|
{
|
2019-10-06 01:29:20 +08:00
|
|
|
status = GIMP_PDB_EXECUTION_ERROR;
|
2008-08-18 04:56:01 +08:00
|
|
|
}
|
2019-10-06 01:29:20 +08:00
|
|
|
}
|
2019-08-24 19:33:54 +08:00
|
|
|
|
|
|
|
if (export == GIMP_EXPORT_EXPORT)
|
2024-04-30 21:50:24 +08:00
|
|
|
gimp_image_delete (image);
|
2019-08-24 19:33:54 +08:00
|
|
|
|
2024-04-30 21:50:24 +08:00
|
|
|
g_list_free (drawables);
|
2019-08-24 19:33:54 +08:00
|
|
|
return gimp_procedure_new_return_values (procedure, status, error);
|
1999-07-07 01:26:06 +08:00
|
|
|
}
|
|
|
|
|
2017-07-07 16:07:24 +08:00
|
|
|
static gboolean
|
|
|
|
diff2_rgb565 (guint8 *ip)
|
|
|
|
{
|
|
|
|
return ip[0] != ip[2] || ip[1] != ip[3];
|
|
|
|
}
|
|
|
|
|
1999-10-05 04:36:06 +08:00
|
|
|
static gboolean
|
|
|
|
diff2_rgb (guint8 *ip)
|
|
|
|
{
|
|
|
|
return ip[0] != ip[3] || ip[1] != ip[4] || ip[2] != ip[5];
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
diff2_rgba (guint8 *ip)
|
|
|
|
{
|
|
|
|
return ip[0] != ip[4] || ip[1] != ip[5] || ip[2] != ip[6] || ip[3] != ip[7];
|
|
|
|
}
|
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
static guint8 *
|
1999-10-05 04:36:06 +08:00
|
|
|
rl_encode_rgbx (guint8 *bp,
|
2008-08-18 04:56:01 +08:00
|
|
|
guint8 *ip,
|
|
|
|
guint8 *limit,
|
2017-07-07 16:07:24 +08:00
|
|
|
guint bpp)
|
1999-10-05 04:36:06 +08:00
|
|
|
{
|
2017-07-07 16:07:24 +08:00
|
|
|
gboolean (*diff2_pix) (guint8 *);
|
|
|
|
guint8 *ilimit = limit - bpp;
|
|
|
|
|
|
|
|
switch (bpp)
|
|
|
|
{
|
|
|
|
case 2: diff2_pix = diff2_rgb565; break;
|
|
|
|
case 3: diff2_pix = diff2_rgb; break;
|
|
|
|
case 4: diff2_pix = diff2_rgba; break;
|
|
|
|
default: g_assert_not_reached ();
|
|
|
|
}
|
1999-10-05 04:36:06 +08:00
|
|
|
|
|
|
|
while (ip < limit)
|
|
|
|
{
|
|
|
|
g_assert (ip < ilimit); /* paranoid */
|
|
|
|
|
|
|
|
if (diff2_pix (ip))
|
2008-08-18 04:56:01 +08:00
|
|
|
{
|
|
|
|
guint8 *s_ip = ip;
|
|
|
|
guint l = 1;
|
|
|
|
|
2017-07-07 16:07:24 +08:00
|
|
|
ip += bpp;
|
2008-08-18 04:56:01 +08:00
|
|
|
while (l < 127 && ip < ilimit && diff2_pix (ip))
|
2017-07-07 16:07:24 +08:00
|
|
|
{ ip += bpp; l += 1; }
|
2008-08-18 04:56:01 +08:00
|
|
|
if (ip == ilimit && l < 127)
|
2017-07-07 16:07:24 +08:00
|
|
|
{ ip += bpp; l += 1; }
|
2008-08-18 04:56:01 +08:00
|
|
|
*(bp++) = l;
|
2017-07-07 16:07:24 +08:00
|
|
|
memcpy (bp, s_ip, l * bpp);
|
|
|
|
bp += l * bpp;
|
2008-08-18 04:56:01 +08:00
|
|
|
}
|
1999-10-05 04:36:06 +08:00
|
|
|
else
|
2008-08-18 04:56:01 +08:00
|
|
|
{
|
|
|
|
guint l = 2;
|
1999-10-05 04:36:06 +08:00
|
|
|
|
2017-07-07 16:07:24 +08:00
|
|
|
ip += bpp;
|
2008-08-18 04:56:01 +08:00
|
|
|
while (l < 127 && ip < ilimit && !diff2_pix (ip))
|
2017-07-07 16:07:24 +08:00
|
|
|
{ ip += bpp; l += 1; }
|
2008-08-18 04:56:01 +08:00
|
|
|
*(bp++) = l | 128;
|
2017-07-07 16:07:24 +08:00
|
|
|
memcpy (bp, ip, bpp);
|
|
|
|
ip += bpp;
|
|
|
|
bp += bpp;
|
2008-08-18 04:56:01 +08:00
|
|
|
}
|
1999-10-05 04:36:06 +08:00
|
|
|
if (ip == ilimit)
|
2008-08-18 04:56:01 +08:00
|
|
|
{
|
|
|
|
*(bp++) = 1;
|
2017-07-07 16:07:24 +08:00
|
|
|
memcpy (bp, ip, bpp);
|
|
|
|
ip += bpp;
|
|
|
|
bp += bpp;
|
2008-08-18 04:56:01 +08:00
|
|
|
}
|
1999-10-05 04:36:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return bp;
|
|
|
|
}
|
|
|
|
|
2013-11-29 07:05:38 +08:00
|
|
|
static gboolean print (GOutputStream *stream,
|
|
|
|
GError **error,
|
|
|
|
const gchar *format,
|
|
|
|
...) G_GNUC_PRINTF (3, 4);
|
1999-10-05 04:36:06 +08:00
|
|
|
|
2013-11-29 07:05:38 +08:00
|
|
|
static gboolean
|
|
|
|
print (GOutputStream *stream,
|
|
|
|
GError **error,
|
|
|
|
const gchar *format,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
gboolean success;
|
1999-10-05 04:36:06 +08:00
|
|
|
|
2013-11-29 07:05:38 +08:00
|
|
|
va_start (args, format);
|
2014-08-12 21:29:34 +08:00
|
|
|
success = g_output_stream_vprintf (stream, NULL, NULL,
|
|
|
|
error, format, args);
|
2013-11-29 07:05:38 +08:00
|
|
|
va_end (args);
|
1999-10-05 04:36:06 +08:00
|
|
|
|
2013-11-29 07:05:38 +08:00
|
|
|
return success;
|
|
|
|
}
|
1999-10-05 04:36:06 +08:00
|
|
|
|
2013-11-29 07:05:38 +08:00
|
|
|
static inline gboolean
|
|
|
|
save_rle_decoder (GOutputStream *output,
|
|
|
|
const gchar *macro_name,
|
|
|
|
const gchar *s_uint,
|
|
|
|
const gchar *s_uint_8,
|
2017-07-07 16:07:24 +08:00
|
|
|
guint bpp,
|
2013-11-29 07:05:38 +08:00
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
print (output, error,
|
|
|
|
"#define %s_RUN_LENGTH_DECODE(image_buf, rle_data, size, bpp) do \\\n",
|
|
|
|
macro_name) &&
|
|
|
|
print (output, error,
|
|
|
|
"{ %s __bpp; %s *__ip; const %s *__il, *__rd; \\\n",
|
|
|
|
s_uint, s_uint_8, s_uint_8) &&
|
|
|
|
print (output, error,
|
|
|
|
" __bpp = (bpp); __ip = (image_buf); __il = __ip + (size) * __bpp; \\\n"
|
|
|
|
" __rd = (rle_data); if (__bpp > 3) { /* RGBA */ \\\n"
|
|
|
|
" while (__ip < __il) { %s __l = *(__rd++); \\\n",
|
|
|
|
s_uint) &&
|
|
|
|
print (output, error,
|
|
|
|
" if (__l & 128) { __l = __l - 128; \\\n"
|
|
|
|
" do { memcpy (__ip, __rd, 4); __ip += 4; } while (--__l); __rd += 4; \\\n"
|
|
|
|
" } else { __l *= 4; memcpy (__ip, __rd, __l); \\\n"
|
|
|
|
" __ip += __l; __rd += __l; } } \\\n"
|
2017-07-07 16:07:24 +08:00
|
|
|
" } else if (__bpp == 3) { /* RGB */ \\\n"
|
2013-11-29 07:05:38 +08:00
|
|
|
" while (__ip < __il) { %s __l = *(__rd++); \\\n",
|
|
|
|
s_uint) &&
|
|
|
|
print (output, error,
|
|
|
|
" if (__l & 128) { __l = __l - 128; \\\n"
|
|
|
|
" do { memcpy (__ip, __rd, 3); __ip += 3; } while (--__l); __rd += 3; \\\n"
|
|
|
|
" } else { __l *= 3; memcpy (__ip, __rd, __l); \\\n"
|
|
|
|
" __ip += __l; __rd += __l; } } \\\n"
|
2017-07-07 16:07:24 +08:00
|
|
|
" } else { /* RGB16 */ \\\n"
|
|
|
|
" while (__ip < __il) { %s __l = *(__rd++); \\\n",
|
|
|
|
s_uint) &&
|
|
|
|
print (output, error,
|
|
|
|
" if (__l & 128) { __l = __l - 128; \\\n"
|
|
|
|
" do { memcpy (__ip, __rd, 2); __ip += 2; } while (--__l); __rd += 2; \\\n"
|
|
|
|
" } else { __l *= 2; memcpy (__ip, __rd, __l); \\\n"
|
|
|
|
" __ip += __l; __rd += __l; } } \\\n"
|
2013-11-29 07:05:38 +08:00
|
|
|
" } } while (0)\n");
|
1999-10-05 04:36:06 +08:00
|
|
|
}
|
|
|
|
|
2013-11-29 07:05:38 +08:00
|
|
|
static inline gboolean
|
|
|
|
save_uchar (GOutputStream *output,
|
|
|
|
guint *c,
|
|
|
|
guint8 d,
|
2019-10-06 01:29:20 +08:00
|
|
|
gboolean use_macros,
|
2013-11-29 07:05:38 +08:00
|
|
|
GError **error)
|
1999-07-07 08:47:19 +08:00
|
|
|
{
|
|
|
|
static guint8 pad = 0;
|
|
|
|
|
2013-11-29 07:05:38 +08:00
|
|
|
if (*c > 74)
|
1999-07-07 08:47:19 +08:00
|
|
|
{
|
2019-10-06 01:29:20 +08:00
|
|
|
if (! use_macros)
|
2008-08-18 04:56:01 +08:00
|
|
|
{
|
2013-11-29 07:05:38 +08:00
|
|
|
if (! print (output, error, "\"\n \""))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
*c = 3;
|
2008-08-18 04:56:01 +08:00
|
|
|
}
|
1999-10-02 20:54:08 +08:00
|
|
|
else
|
2008-08-18 04:56:01 +08:00
|
|
|
{
|
2013-11-29 07:05:38 +08:00
|
|
|
if (! print (output, error, "\"\n \""))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
*c = 2;
|
2008-08-18 04:56:01 +08:00
|
|
|
}
|
1999-07-07 08:47:19 +08:00
|
|
|
}
|
2013-11-29 07:05:38 +08:00
|
|
|
|
2016-04-28 10:46:19 +08:00
|
|
|
if (d < 33 || (d >= 48 && d <= 57) || d > 126)
|
1999-07-07 08:47:19 +08:00
|
|
|
{
|
2016-04-28 10:46:19 +08:00
|
|
|
if (! print (output, error, "\\%03o", d))
|
2013-11-29 07:05:38 +08:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
*c += 1 + 1 + (d > 7) + (d > 63);
|
1999-07-07 08:47:19 +08:00
|
|
|
pad = d < 64;
|
|
|
|
|
2013-11-29 07:05:38 +08:00
|
|
|
return TRUE;
|
1999-07-07 08:47:19 +08:00
|
|
|
}
|
2003-11-06 23:27:05 +08:00
|
|
|
|
1999-07-07 08:47:19 +08:00
|
|
|
if (d == '\\')
|
|
|
|
{
|
2013-11-29 07:05:38 +08:00
|
|
|
if (! print (output, error, "\\\\"))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
*c += 2;
|
1999-07-07 08:47:19 +08:00
|
|
|
}
|
|
|
|
else if (d == '"')
|
|
|
|
{
|
2013-11-29 07:05:38 +08:00
|
|
|
if (! print (output, error, "\\\""))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
*c += 2;
|
1999-07-07 08:47:19 +08:00
|
|
|
}
|
|
|
|
else if (pad && d >= '0' && d <= '9')
|
|
|
|
{
|
2013-11-29 07:05:38 +08:00
|
|
|
if (! print (output, error, "\"\"%c", d))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
*c += 3;
|
1999-07-07 08:47:19 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-29 07:05:38 +08:00
|
|
|
if (! print (output, error, "%c", d))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
*c += 1;
|
1999-07-07 08:47:19 +08:00
|
|
|
}
|
2013-11-29 07:05:38 +08:00
|
|
|
|
1999-07-07 08:47:19 +08:00
|
|
|
pad = 0;
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2013-11-29 07:05:38 +08:00
|
|
|
return TRUE;
|
1999-07-07 08:47:19 +08:00
|
|
|
}
|
|
|
|
|
2008-08-18 04:56:01 +08:00
|
|
|
static gboolean
|
2024-04-13 23:10:25 +08:00
|
|
|
export_image (GFile *file,
|
|
|
|
GimpImage *image,
|
|
|
|
GimpDrawable *drawable,
|
|
|
|
GObject *config,
|
|
|
|
GError **error)
|
1999-07-07 01:26:06 +08:00
|
|
|
{
|
2013-11-29 07:05:38 +08:00
|
|
|
GOutputStream *output;
|
2012-11-19 07:25:10 +08:00
|
|
|
GeglBuffer *buffer;
|
2018-11-27 19:27:20 +08:00
|
|
|
GCancellable *cancellable;
|
2019-08-24 19:33:54 +08:00
|
|
|
GimpImageType drawable_type = gimp_drawable_type (drawable);
|
2012-11-19 07:25:10 +08:00
|
|
|
gchar *s_uint_8, *s_uint, *s_char, *s_null;
|
|
|
|
guint c;
|
|
|
|
gchar *macro_name;
|
|
|
|
guint8 *img_buffer, *img_buffer_end;
|
|
|
|
gchar *basename;
|
|
|
|
guint8 *data, *p;
|
|
|
|
gint width;
|
|
|
|
gint height;
|
|
|
|
gint x, y, pad, n_bytes, bpp;
|
2019-01-03 04:25:07 +08:00
|
|
|
const Babl *drawable_format;
|
2012-11-19 07:25:10 +08:00
|
|
|
gint drawable_bpp;
|
2019-10-06 01:29:20 +08:00
|
|
|
gchar *config_prefixed_name;
|
|
|
|
gchar *config_comment;
|
2019-10-10 07:32:28 +08:00
|
|
|
gboolean config_save_comment;
|
2019-10-06 01:29:20 +08:00
|
|
|
gboolean config_glib_types;
|
|
|
|
gboolean config_save_alpha;
|
|
|
|
gboolean config_rgb565;
|
|
|
|
gboolean config_use_macros;
|
|
|
|
gboolean config_use_rle;
|
|
|
|
gdouble config_opacity;
|
|
|
|
|
|
|
|
g_object_get (config,
|
|
|
|
"prefixed-name", &config_prefixed_name,
|
2020-06-16 05:54:09 +08:00
|
|
|
"gimp-comment", &config_comment,
|
2019-10-10 07:32:28 +08:00
|
|
|
"save-comment", &config_save_comment,
|
2019-10-06 01:29:20 +08:00
|
|
|
"glib-types", &config_glib_types,
|
|
|
|
"save-alpha", &config_save_alpha,
|
|
|
|
"rgb565", &config_rgb565,
|
|
|
|
"use-macros", &config_use_macros,
|
|
|
|
"use-rle", &config_use_rle,
|
|
|
|
"opacity", &config_opacity,
|
|
|
|
NULL);
|
2000-01-26 01:46:56 +08:00
|
|
|
|
2014-10-04 08:44:54 +08:00
|
|
|
output = G_OUTPUT_STREAM (g_file_replace (file,
|
|
|
|
NULL, FALSE, G_FILE_CREATE_NONE,
|
|
|
|
NULL, error));
|
2013-11-29 07:05:38 +08:00
|
|
|
if (output)
|
|
|
|
{
|
|
|
|
GOutputStream *buffered;
|
|
|
|
|
|
|
|
buffered = g_buffered_output_stream_new (output);
|
|
|
|
g_object_unref (output);
|
|
|
|
|
|
|
|
output = buffered;
|
|
|
|
}
|
|
|
|
else
|
2008-08-18 04:56:01 +08:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2000-01-26 01:46:56 +08:00
|
|
|
|
2019-08-24 19:33:54 +08:00
|
|
|
buffer = gimp_drawable_get_buffer (drawable);
|
1999-07-07 01:26:06 +08:00
|
|
|
|
2012-11-19 07:25:10 +08:00
|
|
|
width = gegl_buffer_get_width (buffer);
|
|
|
|
height = gegl_buffer_get_height (buffer);
|
1999-10-05 04:36:06 +08:00
|
|
|
|
2019-08-24 19:33:54 +08:00
|
|
|
if (gimp_drawable_has_alpha (drawable))
|
2019-01-03 04:25:07 +08:00
|
|
|
drawable_format = babl_format ("R'G'B'A u8");
|
|
|
|
else
|
|
|
|
drawable_format = babl_format ("R'G'B' u8");
|
|
|
|
|
|
|
|
drawable_bpp = babl_format_get_bytes_per_pixel (drawable_format);
|
2012-11-19 07:25:10 +08:00
|
|
|
|
2019-10-06 01:29:20 +08:00
|
|
|
bpp = config_rgb565 ? 2 : (config_save_alpha ? 4 : 3);
|
2012-11-19 07:25:10 +08:00
|
|
|
n_bytes = width * height * bpp;
|
2019-01-03 04:25:07 +08:00
|
|
|
pad = width * drawable_bpp;
|
2019-10-06 01:29:20 +08:00
|
|
|
if (config_use_rle)
|
2012-11-19 07:25:10 +08:00
|
|
|
pad = MAX (pad, 130 + n_bytes / 127);
|
2010-05-29 04:20:35 +08:00
|
|
|
|
2012-11-19 07:25:10 +08:00
|
|
|
data = g_new (guint8, pad + n_bytes);
|
|
|
|
p = data + pad;
|
|
|
|
|
|
|
|
for (y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
gegl_buffer_get (buffer, GEGL_RECTANGLE (0, y, width, 1), 1.0,
|
2019-01-03 04:25:07 +08:00
|
|
|
drawable_format, data,
|
2012-11-19 07:25:10 +08:00
|
|
|
GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
|
2010-05-29 04:20:35 +08:00
|
|
|
|
2012-11-19 07:25:10 +08:00
|
|
|
if (bpp == 2)
|
2008-08-18 04:56:01 +08:00
|
|
|
{
|
2012-11-19 07:25:10 +08:00
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
|
|
|
guint8 *d = data + x * drawable_bpp;
|
|
|
|
guint8 r, g, b;
|
|
|
|
gushort rgb16;
|
|
|
|
gdouble alpha = drawable_type == GIMP_RGBA_IMAGE ? d[3] : 0xff;
|
|
|
|
|
2019-10-06 01:29:20 +08:00
|
|
|
alpha *= config_opacity / 25500.0;
|
2012-11-19 07:25:10 +08:00
|
|
|
r = (0.5 + alpha * (gdouble) d[0]);
|
|
|
|
g = (0.5 + alpha * (gdouble) d[1]);
|
|
|
|
b = (0.5 + alpha * (gdouble) d[2]);
|
|
|
|
r >>= 3;
|
|
|
|
g >>= 2;
|
|
|
|
b >>= 3;
|
|
|
|
rgb16 = (r << 11) + (g << 5) + b;
|
|
|
|
*(p++) = (guchar) rgb16;
|
|
|
|
*(p++) = (guchar) (rgb16 >> 8);
|
|
|
|
}
|
2008-08-18 04:56:01 +08:00
|
|
|
}
|
2019-10-06 01:29:20 +08:00
|
|
|
else if (config_save_alpha)
|
2008-08-18 04:56:01 +08:00
|
|
|
{
|
2012-11-19 07:25:10 +08:00
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
|
|
|
guint8 *d = data + x * drawable_bpp;
|
|
|
|
gdouble alpha = drawable_type == GIMP_RGBA_IMAGE ? d[3] : 0xff;
|
|
|
|
|
2019-10-06 01:29:20 +08:00
|
|
|
alpha *= config_opacity / 100.0;
|
2012-11-19 07:25:10 +08:00
|
|
|
*(p++) = d[0];
|
|
|
|
*(p++) = d[1];
|
|
|
|
*(p++) = d[2];
|
|
|
|
*(p++) = alpha + 0.5;
|
|
|
|
}
|
2008-08-18 04:56:01 +08:00
|
|
|
}
|
1999-10-05 04:36:06 +08:00
|
|
|
else
|
2008-08-18 04:56:01 +08:00
|
|
|
{
|
2012-11-19 07:25:10 +08:00
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
|
|
|
guint8 *d = data + x * drawable_bpp;
|
|
|
|
gdouble alpha = drawable_type == GIMP_RGBA_IMAGE ? d[3] : 0xff;
|
|
|
|
|
2019-10-06 01:29:20 +08:00
|
|
|
alpha *= config_opacity / 25500.0;
|
2012-11-19 07:25:10 +08:00
|
|
|
*(p++) = 0.5 + alpha * (gdouble) d[0];
|
|
|
|
*(p++) = 0.5 + alpha * (gdouble) d[1];
|
|
|
|
*(p++) = 0.5 + alpha * (gdouble) d[2];
|
|
|
|
}
|
2008-08-18 04:56:01 +08:00
|
|
|
}
|
1999-10-05 04:36:06 +08:00
|
|
|
}
|
|
|
|
|
2012-11-19 07:25:10 +08:00
|
|
|
img_buffer = data + pad;
|
2019-10-06 01:29:20 +08:00
|
|
|
if (config_use_rle)
|
2012-11-19 07:25:10 +08:00
|
|
|
{
|
|
|
|
img_buffer_end = rl_encode_rgbx (data, img_buffer,
|
|
|
|
img_buffer + n_bytes, bpp);
|
|
|
|
img_buffer = data;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
img_buffer_end = img_buffer + n_bytes;
|
|
|
|
}
|
|
|
|
|
2019-10-06 01:29:20 +08:00
|
|
|
if (! config_use_macros && config_glib_types)
|
1999-07-07 01:26:06 +08:00
|
|
|
{
|
|
|
|
s_uint_8 = "guint8 ";
|
|
|
|
s_uint = "guint ";
|
|
|
|
s_char = "gchar ";
|
|
|
|
s_null = "NULL";
|
|
|
|
}
|
2019-10-06 01:29:20 +08:00
|
|
|
else if (! config_use_macros)
|
1999-07-07 01:26:06 +08:00
|
|
|
{
|
|
|
|
s_uint_8 = "unsigned char";
|
|
|
|
s_uint = "unsigned int ";
|
|
|
|
s_char = "char ";
|
|
|
|
s_null = "(char*) 0";
|
|
|
|
}
|
2019-10-06 01:29:20 +08:00
|
|
|
else if (config_use_macros && config_glib_types)
|
1999-10-02 20:54:08 +08:00
|
|
|
{
|
|
|
|
s_uint_8 = "guint8";
|
|
|
|
s_uint = "guint";
|
|
|
|
s_char = "gchar";
|
|
|
|
s_null = "NULL";
|
|
|
|
}
|
2019-10-06 01:29:20 +08:00
|
|
|
else /* config_use_macros && ! config_glib_types */
|
1999-10-02 20:54:08 +08:00
|
|
|
{
|
|
|
|
s_uint_8 = "unsigned char";
|
|
|
|
s_uint = "unsigned int";
|
|
|
|
s_char = "char";
|
|
|
|
s_null = "(char*) 0";
|
|
|
|
}
|
2001-08-30 01:48:28 +08:00
|
|
|
|
2019-10-06 01:29:20 +08:00
|
|
|
macro_name = g_ascii_strup (config_prefixed_name, -1);
|
2001-08-30 01:48:28 +08:00
|
|
|
|
2013-11-29 07:05:38 +08:00
|
|
|
basename = g_file_get_basename (file);
|
2001-08-30 01:48:28 +08:00
|
|
|
|
2013-11-29 07:05:38 +08:00
|
|
|
if (! print (output, error,
|
|
|
|
"/* GIMP %s C-Source image dump %s(%s) */\n\n",
|
2019-10-06 01:29:20 +08:00
|
|
|
config_save_alpha ? "RGBA" : "RGB",
|
|
|
|
config_use_rle ? "1-byte-run-length-encoded " : "",
|
2013-11-29 07:05:38 +08:00
|
|
|
basename))
|
|
|
|
goto fail;
|
2001-08-30 01:48:28 +08:00
|
|
|
|
|
|
|
g_free (basename);
|
1999-07-07 01:26:06 +08:00
|
|
|
|
2019-10-06 01:29:20 +08:00
|
|
|
if (config_use_rle && !config_use_macros)
|
2013-11-29 07:05:38 +08:00
|
|
|
{
|
|
|
|
if (! save_rle_decoder (output,
|
|
|
|
macro_name,
|
2019-10-06 01:29:20 +08:00
|
|
|
config_glib_types ? "guint" : "unsigned int",
|
|
|
|
config_glib_types ? "guint8" : "unsigned char",
|
2017-07-07 16:07:24 +08:00
|
|
|
bpp,
|
2013-11-29 07:05:38 +08:00
|
|
|
error))
|
|
|
|
goto fail;
|
|
|
|
}
|
1999-10-05 04:36:06 +08:00
|
|
|
|
2019-10-06 01:29:20 +08:00
|
|
|
if (!config_use_macros)
|
1999-10-02 20:54:08 +08:00
|
|
|
{
|
2013-11-29 07:05:38 +08:00
|
|
|
if (! print (output, error,
|
|
|
|
"static const struct {\n"
|
|
|
|
" %s\t width;\n"
|
|
|
|
" %s\t height;\n"
|
|
|
|
" %s\t bytes_per_pixel; /* 2:RGB16, 3:RGB, 4:RGBA */ \n",
|
|
|
|
s_uint, s_uint, s_uint))
|
|
|
|
goto fail;
|
|
|
|
|
2019-10-10 07:32:28 +08:00
|
|
|
if (config_save_comment)
|
2013-11-29 07:05:38 +08:00
|
|
|
{
|
|
|
|
if (! print (output, error, " %s\t*comment;\n", s_char))
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! print (output, error,
|
|
|
|
" %s\t %spixel_data[",
|
|
|
|
s_uint_8,
|
2019-10-06 01:29:20 +08:00
|
|
|
config_use_rle ? "rle_" : ""))
|
2013-11-29 07:05:38 +08:00
|
|
|
goto fail;
|
|
|
|
|
2019-10-06 01:29:20 +08:00
|
|
|
if (config_use_rle)
|
2013-11-29 07:05:38 +08:00
|
|
|
{
|
|
|
|
if (! print (output, error,
|
|
|
|
"%u + 1];\n",
|
|
|
|
(guint) (img_buffer_end - img_buffer)))
|
|
|
|
goto fail;
|
|
|
|
}
|
1999-10-05 04:36:06 +08:00
|
|
|
else
|
2013-11-29 07:05:38 +08:00
|
|
|
{
|
|
|
|
if (! print (output, error,
|
|
|
|
"%u * %u * %u + 1];\n",
|
|
|
|
width,
|
|
|
|
height,
|
2017-07-07 15:45:20 +08:00
|
|
|
bpp))
|
2013-11-29 07:05:38 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-10-06 01:29:20 +08:00
|
|
|
if (! print (output, error, "} %s = {\n", config_prefixed_name))
|
2013-11-29 07:05:38 +08:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (! print (output, error,
|
|
|
|
" %u, %u, %u,\n",
|
|
|
|
width,
|
|
|
|
height,
|
2017-07-07 15:45:20 +08:00
|
|
|
bpp))
|
2013-11-29 07:05:38 +08:00
|
|
|
goto fail;
|
1999-10-02 20:54:08 +08:00
|
|
|
}
|
|
|
|
else /* use macros */
|
|
|
|
{
|
2013-11-29 07:05:38 +08:00
|
|
|
if (! print (output, error,
|
|
|
|
"#define %s_WIDTH (%u)\n"
|
|
|
|
"#define %s_HEIGHT (%u)\n"
|
2017-07-07 15:45:20 +08:00
|
|
|
"#define %s_BYTES_PER_PIXEL (%u) /* 2:RGB16, 3:RGB, 4:RGBA */\n",
|
2013-11-29 07:05:38 +08:00
|
|
|
macro_name, width,
|
|
|
|
macro_name, height,
|
2017-07-07 15:45:20 +08:00
|
|
|
macro_name, bpp))
|
2013-11-29 07:05:38 +08:00
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
1999-10-02 20:54:08 +08:00
|
|
|
}
|
2013-11-29 07:05:38 +08:00
|
|
|
|
2019-10-10 07:32:28 +08:00
|
|
|
if (config_save_comment && ! config_comment)
|
1999-10-02 20:54:08 +08:00
|
|
|
{
|
2019-10-06 01:29:20 +08:00
|
|
|
if (! config_use_macros)
|
2013-11-29 07:05:38 +08:00
|
|
|
{
|
|
|
|
if (! print (output, error, " %s,\n", s_null))
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (! print (output, error,
|
|
|
|
"#define %s_COMMENT (%s)\n",
|
|
|
|
macro_name, s_null))
|
|
|
|
goto fail;
|
|
|
|
}
|
1999-10-02 20:54:08 +08:00
|
|
|
}
|
2019-10-10 07:32:28 +08:00
|
|
|
else if (config_save_comment)
|
1999-07-07 01:26:06 +08:00
|
|
|
{
|
2019-10-06 01:29:20 +08:00
|
|
|
gchar *p = config_comment - 1;
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2019-10-06 01:29:20 +08:00
|
|
|
if (config_use_macros)
|
2013-11-29 07:05:38 +08:00
|
|
|
{
|
|
|
|
if (! print (output, error, "#define %s_COMMENT \\\n", macro_name))
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! print (output, error, " \""))
|
|
|
|
goto fail;
|
|
|
|
|
1999-07-07 01:26:06 +08:00
|
|
|
while (*(++p))
|
2013-11-29 07:05:38 +08:00
|
|
|
{
|
|
|
|
gboolean success = FALSE;
|
|
|
|
|
|
|
|
if (*p == '\\')
|
|
|
|
success = print (output, error, "\\\\");
|
|
|
|
else if (*p == '"')
|
|
|
|
success = print (output, error, "\\\"");
|
|
|
|
else if (*p == '\n' && p[1])
|
|
|
|
success = print (output, error,
|
|
|
|
"\\n\"%s\n \"",
|
2019-10-06 01:29:20 +08:00
|
|
|
config_use_macros ? " \\" : "");
|
2013-11-29 07:05:38 +08:00
|
|
|
else if (*p == '\n')
|
|
|
|
success = print (output, error, "\\n");
|
|
|
|
else if (*p == '\r')
|
|
|
|
success = print (output, error, "\\r");
|
|
|
|
else if (*p == '\b')
|
|
|
|
success = print (output, error, "\\b");
|
|
|
|
else if (*p == '\f')
|
|
|
|
success = print (output, error, "\\f");
|
2016-04-28 10:46:19 +08:00
|
|
|
else if (( *p >= 32 && *p <= 47 ) || (*p >= 58 && *p <= 126))
|
2013-11-29 07:05:38 +08:00
|
|
|
success = print (output, error, "%c", *p);
|
|
|
|
else
|
|
|
|
success = print (output, error, "\\%03o", *p);
|
|
|
|
|
|
|
|
if (! success)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-10-06 01:29:20 +08:00
|
|
|
if (! config_use_macros)
|
2013-11-29 07:05:38 +08:00
|
|
|
{
|
|
|
|
if (! print (output, error, "\",\n"))
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (! print (output, error, "\"\n"))
|
|
|
|
goto fail;
|
|
|
|
}
|
1999-10-02 20:54:08 +08:00
|
|
|
}
|
2013-11-29 07:05:38 +08:00
|
|
|
|
2019-10-06 01:29:20 +08:00
|
|
|
if (config_use_macros)
|
1999-10-02 20:54:08 +08:00
|
|
|
{
|
2013-11-29 07:05:38 +08:00
|
|
|
if (! print (output, error,
|
|
|
|
"#define %s_%sPIXEL_DATA ((%s*) %s_%spixel_data)\n",
|
|
|
|
macro_name,
|
2019-10-06 01:29:20 +08:00
|
|
|
config_use_rle ? "RLE_" : "",
|
2013-11-29 07:05:38 +08:00
|
|
|
s_uint_8,
|
|
|
|
macro_name,
|
2019-10-06 01:29:20 +08:00
|
|
|
config_use_rle ? "rle_" : ""))
|
2013-11-29 07:05:38 +08:00
|
|
|
goto fail;
|
|
|
|
|
2019-10-06 01:29:20 +08:00
|
|
|
if (config_use_rle)
|
2013-11-29 07:05:38 +08:00
|
|
|
{
|
|
|
|
if (! save_rle_decoder (output,
|
|
|
|
macro_name,
|
|
|
|
s_uint,
|
|
|
|
s_uint_8,
|
2017-07-07 16:07:24 +08:00
|
|
|
bpp,
|
2013-11-29 07:05:38 +08:00
|
|
|
error))
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! print (output, error,
|
|
|
|
"static const %s %s_%spixel_data[",
|
|
|
|
s_uint_8,
|
|
|
|
macro_name,
|
2019-10-06 01:29:20 +08:00
|
|
|
config_use_rle ? "rle_" : ""))
|
2013-11-29 07:05:38 +08:00
|
|
|
goto fail;
|
|
|
|
|
2019-10-06 01:29:20 +08:00
|
|
|
if (config_use_rle)
|
2013-11-29 07:05:38 +08:00
|
|
|
{
|
|
|
|
if (! print (output, error,
|
2020-07-16 20:45:20 +08:00
|
|
|
"%u + 1] =\n",
|
2013-11-29 07:05:38 +08:00
|
|
|
(guint) (img_buffer_end - img_buffer)))
|
|
|
|
goto fail;
|
|
|
|
}
|
1999-10-05 04:36:06 +08:00
|
|
|
else
|
2013-11-29 07:05:38 +08:00
|
|
|
{
|
|
|
|
if (! print (output, error,
|
|
|
|
"%u * %u * %u + 1] =\n",
|
|
|
|
width,
|
|
|
|
height,
|
2017-07-07 15:45:20 +08:00
|
|
|
bpp))
|
2013-11-29 07:05:38 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! print (output, error, "(\""))
|
|
|
|
goto fail;
|
|
|
|
|
1999-10-02 20:54:08 +08:00
|
|
|
c = 2;
|
1999-07-07 01:26:06 +08:00
|
|
|
}
|
1999-07-07 08:47:19 +08:00
|
|
|
else
|
1999-10-02 20:54:08 +08:00
|
|
|
{
|
2013-11-29 07:05:38 +08:00
|
|
|
if (! print (output, error, " \""))
|
|
|
|
goto fail;
|
|
|
|
|
1999-10-02 20:54:08 +08:00
|
|
|
c = 3;
|
|
|
|
}
|
2013-11-29 07:05:38 +08:00
|
|
|
|
1999-07-07 01:26:06 +08:00
|
|
|
switch (drawable_type)
|
|
|
|
{
|
2000-08-22 09:26:57 +08:00
|
|
|
case GIMP_RGB_IMAGE:
|
|
|
|
case GIMP_RGBA_IMAGE:
|
1999-10-05 04:36:06 +08:00
|
|
|
do
|
2013-11-29 07:05:38 +08:00
|
|
|
{
|
2019-10-06 01:29:20 +08:00
|
|
|
if (! save_uchar (output, &c, *(img_buffer++), config_use_macros,
|
|
|
|
error))
|
2013-11-29 07:05:38 +08:00
|
|
|
goto fail;
|
|
|
|
}
|
1999-10-05 04:36:06 +08:00
|
|
|
while (img_buffer < img_buffer_end);
|
1999-07-07 01:26:06 +08:00
|
|
|
break;
|
2013-11-29 07:05:38 +08:00
|
|
|
|
1999-07-07 01:26:06 +08:00
|
|
|
default:
|
|
|
|
g_warning ("unhandled drawable type (%d)", drawable_type);
|
2013-11-29 07:05:38 +08:00
|
|
|
goto fail;
|
1999-07-07 01:26:06 +08:00
|
|
|
}
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2019-10-06 01:29:20 +08:00
|
|
|
if (! config_use_macros)
|
2013-11-29 07:05:38 +08:00
|
|
|
{
|
|
|
|
if (! print (output, error, "\",\n};\n\n"))
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (! print (output, error, "\");\n\n"))
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! g_output_stream_close (output, NULL, error))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
g_object_unref (output);
|
2012-11-19 07:25:10 +08:00
|
|
|
g_object_unref (buffer);
|
2003-11-06 23:27:05 +08:00
|
|
|
|
1999-07-07 01:26:06 +08:00
|
|
|
return TRUE;
|
2013-11-29 07:05:38 +08:00
|
|
|
|
|
|
|
fail:
|
|
|
|
|
2018-11-27 19:27:20 +08:00
|
|
|
cancellable = g_cancellable_new ();
|
|
|
|
g_cancellable_cancel (cancellable);
|
|
|
|
g_output_stream_close (output, cancellable, NULL);
|
|
|
|
g_object_unref (cancellable);
|
|
|
|
|
2013-11-29 07:05:38 +08:00
|
|
|
g_object_unref (output);
|
|
|
|
g_object_unref (buffer);
|
|
|
|
|
|
|
|
return FALSE;
|
1999-07-07 01:26:06 +08:00
|
|
|
}
|
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
static gboolean
|
2023-05-20 22:30:41 +08:00
|
|
|
save_dialog (GimpImage *image,
|
|
|
|
GimpProcedure *procedure,
|
2019-10-06 01:29:20 +08:00
|
|
|
GObject *config)
|
2000-01-26 01:46:56 +08:00
|
|
|
{
|
2019-10-06 01:29:20 +08:00
|
|
|
GtkWidget *dialog;
|
|
|
|
GtkWidget *vbox;
|
|
|
|
gboolean run;
|
|
|
|
|
2024-04-20 11:08:57 +08:00
|
|
|
dialog = gimp_export_procedure_dialog_new (GIMP_EXPORT_PROCEDURE (procedure),
|
|
|
|
GIMP_PROCEDURE_CONFIG (config),
|
|
|
|
image);
|
2005-09-10 02:38:00 +08:00
|
|
|
|
2023-03-30 10:04:00 +08:00
|
|
|
gimp_procedure_dialog_get_scale_entry (GIMP_PROCEDURE_DIALOG (dialog),
|
|
|
|
"opacity", 1.0);
|
|
|
|
|
|
|
|
gimp_procedure_dialog_set_sensitive (GIMP_PROCEDURE_DIALOG (dialog),
|
|
|
|
"save-alpha", TRUE, config, "rgb565",
|
|
|
|
TRUE);
|
|
|
|
|
|
|
|
vbox = gimp_procedure_dialog_fill_box (GIMP_PROCEDURE_DIALOG (dialog),
|
|
|
|
"csource-box",
|
|
|
|
"prefixed-name", "gimp-comment",
|
|
|
|
"save-comment", "glib-types",
|
|
|
|
"use-macros", "use-rle", "save-alpha",
|
|
|
|
"rgb565", "opacity",
|
|
|
|
NULL);
|
|
|
|
gtk_box_set_spacing (GTK_BOX (vbox), 12);
|
2004-05-19 05:21:23 +08:00
|
|
|
gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
|
2023-03-30 10:04:00 +08:00
|
|
|
|
|
|
|
gimp_procedure_dialog_fill (GIMP_PROCEDURE_DIALOG (dialog),
|
|
|
|
"csource-box",
|
|
|
|
NULL);
|
1999-07-07 01:26:06 +08:00
|
|
|
gtk_widget_show (dialog);
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2019-10-06 01:29:20 +08:00
|
|
|
run = gimp_procedure_dialog_run (GIMP_PROCEDURE_DIALOG (dialog));
|
2003-11-06 23:27:05 +08:00
|
|
|
|
|
|
|
gtk_widget_destroy (dialog);
|
|
|
|
|
|
|
|
return run;
|
1999-07-07 01:26:06 +08:00
|
|
|
}
|