2006-12-09 21:33:38 +00:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
2000-01-25 17:46:56 +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
|
2000-01-25 17:46:56 +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
|
2000-01-25 17:46:56 +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/>.
|
2000-01-25 17:46:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
1999-05-29 01:28:24 +00:00
|
|
|
#include <string.h>
|
2000-01-25 17:46:56 +00:00
|
|
|
|
|
|
|
#include <libgimp/gimp.h>
|
|
|
|
#include <libgimp/gimpui.h>
|
|
|
|
|
2000-01-17 13:21:13 +00:00
|
|
|
#include "libgimp/stdplugins-intl.h"
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2000-05-01 17:01:18 +00:00
|
|
|
|
2024-04-13 15:10:25 +00:00
|
|
|
#define EXPORT_PROC "file-header-export"
|
2008-08-11 10:06:13 +00:00
|
|
|
#define PLUG_IN_BINARY "file-header"
|
2011-04-08 20:31:34 +02:00
|
|
|
#define PLUG_IN_ROLE "gimp-file-header"
|
2005-08-13 22:52:41 +00:00
|
|
|
|
|
|
|
|
2019-08-24 13:41:48 +02:00
|
|
|
typedef struct _Header Header;
|
|
|
|
typedef struct _HeaderClass HeaderClass;
|
2015-10-17 23:28:45 +02:00
|
|
|
|
2019-08-24 13:41:48 +02:00
|
|
|
struct _Header
|
|
|
|
{
|
|
|
|
GimpPlugIn parent_instance;
|
|
|
|
};
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-08-24 13:41:48 +02:00
|
|
|
struct _HeaderClass
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2019-08-24 13:41:48 +02:00
|
|
|
GimpPlugInClass parent_class;
|
1997-11-24 22:05:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-08-24 13:41:48 +02:00
|
|
|
#define HEADER_TYPE (header_get_type ())
|
2023-10-18 18:29:37 +02:00
|
|
|
#define HEADER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), HEADER_TYPE, Header))
|
2019-08-24 13:41:48 +02:00
|
|
|
|
|
|
|
GType header_get_type (void) G_GNUC_CONST;
|
|
|
|
|
|
|
|
static GList * header_query_procedures (GimpPlugIn *plug_in);
|
|
|
|
static GimpProcedure * header_create_procedure (GimpPlugIn *plug_in,
|
|
|
|
const gchar *name);
|
|
|
|
|
2024-04-13 15:10:25 +00:00
|
|
|
static GimpValueArray * header_export (GimpProcedure *procedure,
|
2019-08-24 13:41:48 +02:00
|
|
|
GimpRunMode run_mode,
|
|
|
|
GimpImage *image,
|
|
|
|
GFile *file,
|
2024-05-06 18:38:12 +00:00
|
|
|
GimpExportOptions *options,
|
2023-07-21 18:08:00 +02:00
|
|
|
GimpMetadata *metadata,
|
|
|
|
GimpProcedureConfig *config,
|
2019-08-24 13:41:48 +02:00
|
|
|
gpointer run_data);
|
|
|
|
|
2024-04-13 15:10:25 +00:00
|
|
|
static gboolean export_image (GFile *file,
|
2019-08-24 13:41:48 +02:00
|
|
|
GimpImage *image,
|
|
|
|
GimpDrawable *drawable,
|
|
|
|
GError **error);
|
|
|
|
|
|
|
|
static gboolean print (GOutputStream *output,
|
|
|
|
GError **error,
|
|
|
|
const gchar *format,
|
|
|
|
...) G_GNUC_PRINTF (3, 4);
|
|
|
|
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (Header, header, GIMP_TYPE_PLUG_IN)
|
|
|
|
|
|
|
|
GIMP_MAIN (HEADER_TYPE)
|
2022-05-26 00:59:36 +02:00
|
|
|
DEFINE_STD_SET_I18N
|
2019-08-24 13:41:48 +02:00
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
|
|
|
|
static void
|
2019-08-24 13:41:48 +02:00
|
|
|
header_class_init (HeaderClass *klass)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2019-08-24 13:41:48 +02:00
|
|
|
GimpPlugInClass *plug_in_class = GIMP_PLUG_IN_CLASS (klass);
|
|
|
|
|
|
|
|
plug_in_class->query_procedures = header_query_procedures;
|
|
|
|
plug_in_class->create_procedure = header_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 13:41:48 +02:00
|
|
|
header_init (Header *header)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2019-08-24 13:41:48 +02:00
|
|
|
}
|
2012-11-19 00:34:36 +01:00
|
|
|
|
2019-08-24 13:41:48 +02:00
|
|
|
static GList *
|
|
|
|
header_query_procedures (GimpPlugIn *plug_in)
|
|
|
|
{
|
2024-04-13 15:10:25 +00:00
|
|
|
return g_list_append (NULL, g_strdup (EXPORT_PROC));
|
2019-08-24 13:41:48 +02:00
|
|
|
}
|
2003-03-25 16:38:19 +00:00
|
|
|
|
2019-08-24 13:41:48 +02:00
|
|
|
static GimpProcedure *
|
|
|
|
header_create_procedure (GimpPlugIn *plug_in,
|
|
|
|
const gchar *name)
|
|
|
|
{
|
|
|
|
GimpProcedure *procedure = NULL;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2024-04-13 15:10:25 +00:00
|
|
|
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, header_export, NULL, NULL);
|
2019-08-24 13:41:48 +02:00
|
|
|
|
|
|
|
gimp_procedure_set_image_types (procedure, "INDEXED, RGB");
|
|
|
|
|
2022-07-04 22:50:53 +02:00
|
|
|
gimp_procedure_set_menu_label (procedure, _("C source code header"));
|
2019-08-24 13:41:48 +02:00
|
|
|
|
|
|
|
gimp_procedure_set_documentation (procedure,
|
2024-05-06 18:38:12 +00:00
|
|
|
_("Saves files as C unsigned character "
|
|
|
|
"array"),
|
2019-08-24 13:41:48 +02:00
|
|
|
"FIXME: write help",
|
|
|
|
name);
|
|
|
|
gimp_procedure_set_attribution (procedure,
|
|
|
|
"Spencer Kimball & Peter Mattis",
|
|
|
|
"Spencer Kimball & Peter Mattis",
|
|
|
|
"1997");
|
|
|
|
|
|
|
|
gimp_file_procedure_set_handles_remote (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
TRUE);
|
|
|
|
gimp_file_procedure_set_mime_types (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"image/x-chdr");
|
|
|
|
gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
|
|
|
|
"h");
|
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_INDEXED,
|
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);
|
2019-08-24 13:41:48 +02:00
|
|
|
}
|
2013-11-10 00:18:48 +01:00
|
|
|
|
2019-08-24 13:41:48 +02:00
|
|
|
return procedure;
|
|
|
|
}
|
2008-10-20 06:04:39 +00:00
|
|
|
|
2019-08-24 13:41:48 +02:00
|
|
|
static GimpValueArray *
|
2024-04-13 15:10:25 +00:00
|
|
|
header_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 13:41:48 +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;
|
2019-08-24 13:41:48 +02:00
|
|
|
GError *error = NULL;
|
1999-10-09 19:06:14 +00:00
|
|
|
|
2019-08-24 13:41:48 +02:00
|
|
|
gegl_init (NULL, NULL);
|
1999-10-09 19:06:14 +00:00
|
|
|
|
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);
|
2000-01-25 17:46:56 +00:00
|
|
|
|
2024-04-30 13:50:24 +00:00
|
|
|
if (! export_image (file, image, drawables->data,
|
2024-04-13 15:10:25 +00:00
|
|
|
&error))
|
2014-10-03 23:40:13 +02:00
|
|
|
{
|
2019-08-24 13:41:48 +02:00
|
|
|
status = GIMP_PDB_EXECUTION_ERROR;
|
2014-10-03 23:40:13 +02:00
|
|
|
}
|
|
|
|
|
2019-08-24 13:41:48 +02:00
|
|
|
if (export == GIMP_EXPORT_EXPORT)
|
2024-04-30 13:50:24 +00:00
|
|
|
gimp_image_delete (image);
|
2019-08-24 13:41:48 +02:00
|
|
|
|
2024-04-30 13:50:24 +00:00
|
|
|
g_list_free (drawables);
|
2019-08-24 13:41:48 +02:00
|
|
|
return gimp_procedure_new_return_values (procedure, status, error);
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
|
|
|
|
2014-10-03 23:40:13 +02:00
|
|
|
static gboolean
|
2024-04-13 15:10:25 +00:00
|
|
|
export_image (GFile *file,
|
|
|
|
GimpImage *image,
|
|
|
|
GimpDrawable *drawable,
|
|
|
|
GError **error)
|
1997-11-24 22:05:25 +00:00
|
|
|
{
|
2012-11-19 00:34:36 +01:00
|
|
|
GeglBuffer *buffer;
|
|
|
|
const Babl *format;
|
2003-07-02 12:03:08 +00:00
|
|
|
GimpImageType drawable_type;
|
2014-10-03 23:40:13 +02:00
|
|
|
GOutputStream *output;
|
2008-09-17 08:37:24 +00:00
|
|
|
gint x, y, b, c;
|
|
|
|
const gchar *backslash = "\\\\";
|
|
|
|
const gchar *quote = "\\\"";
|
|
|
|
const gchar *newline = "\"\n\t\"";
|
|
|
|
gchar buf[4];
|
|
|
|
guchar *d = NULL;
|
2011-01-26 08:05:34 +01:00
|
|
|
guchar *data = NULL;
|
2008-09-17 08:37:24 +00:00
|
|
|
guchar *cmap;
|
2018-11-27 12:27:20 +01:00
|
|
|
GCancellable *cancellable;
|
2008-09-17 08:37:24 +00:00
|
|
|
gint colors;
|
2012-11-19 00:34:36 +01:00
|
|
|
gint width;
|
|
|
|
gint height;
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2014-10-03 23:40:13 +02:00
|
|
|
output = G_OUTPUT_STREAM (g_file_replace (file,
|
|
|
|
NULL, FALSE, G_FILE_CREATE_NONE,
|
|
|
|
NULL, error));
|
|
|
|
if (output)
|
|
|
|
{
|
|
|
|
GOutputStream *buffered;
|
|
|
|
|
|
|
|
buffered = g_buffered_output_stream_new (output);
|
|
|
|
g_object_unref (output);
|
|
|
|
|
|
|
|
output = buffered;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2019-08-24 13:41:48 +02:00
|
|
|
buffer = gimp_drawable_get_buffer (drawable);
|
2012-11-19 00:34:36 +01:00
|
|
|
|
|
|
|
width = gegl_buffer_get_width (buffer);
|
|
|
|
height = gegl_buffer_get_height (buffer);
|
|
|
|
|
2019-08-24 13:41:48 +02:00
|
|
|
drawable_type = gimp_drawable_type (drawable);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2014-10-04 02:30:37 +02:00
|
|
|
if (! print (output, error,
|
|
|
|
"/* GIMP header image file format (%s): %s */\n\n",
|
|
|
|
GIMP_RGB_IMAGE == drawable_type ? "RGB" : "INDEXED",
|
|
|
|
gimp_file_get_utf8_name (file)) ||
|
|
|
|
! print (output, error,
|
|
|
|
"static unsigned int width = %d;\n", width) ||
|
|
|
|
! print (output, error,
|
|
|
|
"static unsigned int height = %d;\n\n", height) ||
|
|
|
|
! print (output, error,
|
|
|
|
"/* Call this macro repeatedly. After each use, the pixel data can be extracted */\n\n"))
|
2014-10-03 23:40:13 +02:00
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
2008-09-17 08:37:24 +00:00
|
|
|
|
1999-06-28 17:54:19 +00:00
|
|
|
switch (drawable_type)
|
|
|
|
{
|
2000-08-22 01:26:57 +00:00
|
|
|
case GIMP_RGB_IMAGE:
|
2014-10-04 02:30:37 +02:00
|
|
|
if (! print (output, error,
|
|
|
|
"#define HEADER_PIXEL(data,pixel) {\\\n"
|
|
|
|
"pixel[0] = (((data[0] - 33) << 2) | ((data[1] - 33) >> 4)); \\\n"
|
|
|
|
"pixel[1] = ((((data[1] - 33) & 0xF) << 4) | ((data[2] - 33) >> 2)); \\\n"
|
|
|
|
"pixel[2] = ((((data[2] - 33) & 0x3) << 6) | ((data[3] - 33))); \\\n"
|
|
|
|
"data += 4; \\\n}\n") ||
|
|
|
|
! print (output, error,
|
|
|
|
"static char *header_data =\n\t\""))
|
2014-10-03 23:40:13 +02:00
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2012-11-19 00:34:36 +01:00
|
|
|
format = babl_format ("R'G'B' u8");
|
|
|
|
|
|
|
|
data = g_new (guchar, width * babl_format_get_bytes_per_pixel (format));
|
|
|
|
|
1999-06-28 17:54:19 +00:00
|
|
|
c = 0;
|
2012-11-19 00:34:36 +01:00
|
|
|
for (y = 0; y < height; y++)
|
2008-10-20 06:04:39 +00:00
|
|
|
{
|
2012-11-19 00:34:36 +01:00
|
|
|
gegl_buffer_get (buffer, GEGL_RECTANGLE (0, y, width, 1), 1.0,
|
|
|
|
format, data,
|
|
|
|
GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
|
2008-09-17 08:37:24 +00:00
|
|
|
|
2012-11-19 00:34:36 +01:00
|
|
|
for (x = 0; x < width; x++)
|
2008-10-20 06:04:39 +00:00
|
|
|
{
|
2012-11-19 00:34:36 +01:00
|
|
|
d = data + x * babl_format_get_bytes_per_pixel (format);
|
2003-11-06 15:27:05 +00:00
|
|
|
|
2008-10-20 06:04:39 +00:00
|
|
|
buf[0] = ((d[0] >> 2) & 0x3F) + 33;
|
|
|
|
buf[1] = ((((d[0] & 0x3) << 4) | (d[1] >> 4)) & 0x3F) + 33;
|
|
|
|
buf[2] = ((((d[1] & 0xF) << 2) | (d[2] >> 6)) & 0x3F) + 33;
|
|
|
|
buf[3] = (d[2] & 0x3F) + 33;
|
2003-11-06 15:27:05 +00:00
|
|
|
|
2008-10-20 06:04:39 +00:00
|
|
|
for (b = 0; b < 4; b++)
|
2008-09-17 08:37:24 +00:00
|
|
|
{
|
|
|
|
if (buf[b] == '"')
|
2014-10-03 23:40:13 +02:00
|
|
|
{
|
2014-10-04 02:30:37 +02:00
|
|
|
if (! print (output, error, "%s", quote))
|
2014-10-03 23:40:13 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2008-09-17 08:37:24 +00:00
|
|
|
else if (buf[b] == '\\')
|
2014-10-03 23:40:13 +02:00
|
|
|
{
|
2014-10-04 02:30:37 +02:00
|
|
|
if (! print (output, error, "%s", backslash))
|
2014-10-03 23:40:13 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2008-09-17 08:37:24 +00:00
|
|
|
else
|
2014-10-03 23:40:13 +02:00
|
|
|
{
|
2014-10-04 02:30:37 +02:00
|
|
|
if (! print (output, error, "%c", buf[b]))
|
2014-10-03 23:40:13 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2008-09-17 08:37:24 +00:00
|
|
|
}
|
2003-11-06 15:27:05 +00:00
|
|
|
|
2008-10-20 06:04:39 +00:00
|
|
|
c++;
|
|
|
|
if (c >= 16)
|
|
|
|
{
|
2014-10-04 02:30:37 +02:00
|
|
|
if (! print (output, error, "%s", newline))
|
2014-10-03 23:40:13 +02:00
|
|
|
goto fail;
|
|
|
|
|
2008-10-20 06:04:39 +00:00
|
|
|
c = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-09-17 08:37:24 +00:00
|
|
|
|
2014-10-04 02:30:37 +02:00
|
|
|
if (! print (output, error, "\";\n"))
|
2014-10-03 23:40:13 +02:00
|
|
|
goto fail;
|
1999-06-28 17:54:19 +00:00
|
|
|
break;
|
|
|
|
|
2000-08-22 01:26:57 +00:00
|
|
|
case GIMP_INDEXED_IMAGE:
|
2014-10-04 02:30:37 +02:00
|
|
|
if (! print (output, error,
|
|
|
|
"#define HEADER_PIXEL(data,pixel) {\\\n"
|
|
|
|
"pixel[0] = header_data_cmap[(unsigned char)data[0]][0]; \\\n"
|
|
|
|
"pixel[1] = header_data_cmap[(unsigned char)data[0]][1]; \\\n"
|
|
|
|
"pixel[2] = header_data_cmap[(unsigned char)data[0]][2]; \\\n"
|
|
|
|
"data ++; }\n\n"))
|
2014-10-03 23:40:13 +02:00
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
1999-06-28 17:54:19 +00:00
|
|
|
/* save colormap */
|
2024-09-23 15:17:04 +02:00
|
|
|
cmap = gimp_palette_get_colormap (gimp_image_get_palette (image), babl_format ("R'G'B' u8"), &colors, NULL);
|
1999-06-28 17:54:19 +00:00
|
|
|
|
2014-10-04 02:30:37 +02:00
|
|
|
if (! print (output, error,
|
2019-12-09 06:03:23 +00:00
|
|
|
"static unsigned char header_data_cmap[256][3] = {") ||
|
2014-10-04 02:30:37 +02:00
|
|
|
! print (output, error,
|
|
|
|
"\n\t{%3d,%3d,%3d}",
|
|
|
|
(gint) cmap[0], (gint) cmap[1], (gint) cmap[2]))
|
2014-10-03 23:40:13 +02:00
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
2008-09-17 08:37:24 +00:00
|
|
|
|
1999-06-28 17:54:19 +00:00
|
|
|
for (c = 1; c < colors; c++)
|
2014-10-03 23:40:13 +02:00
|
|
|
{
|
2014-10-04 02:30:37 +02:00
|
|
|
if (! print (output, error,
|
|
|
|
",\n\t{%3d,%3d,%3d}",
|
|
|
|
(gint) cmap[3 * c],
|
|
|
|
(gint) cmap[3 * c + 1],
|
|
|
|
(gint) cmap[3 * c + 2]))
|
2014-10-03 23:40:13 +02:00
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
2008-09-17 08:37:24 +00:00
|
|
|
|
1999-06-28 17:54:19 +00:00
|
|
|
/* fill the rest */
|
|
|
|
for ( ; c < 256; c++)
|
2014-10-03 23:40:13 +02:00
|
|
|
{
|
2014-10-04 02:30:37 +02:00
|
|
|
if (! print (output, error, ",\n\t{255,255,255}"))
|
2014-10-03 23:40:13 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2008-09-17 08:37:24 +00:00
|
|
|
|
1999-06-28 17:54:19 +00:00
|
|
|
/* close bracket */
|
2014-10-04 02:30:37 +02:00
|
|
|
if (! print (output, error, "\n\t};\n"))
|
2014-10-03 23:40:13 +02:00
|
|
|
goto fail;
|
|
|
|
|
2008-09-17 08:37:24 +00:00
|
|
|
g_free (cmap);
|
1999-06-28 17:54:19 +00:00
|
|
|
|
|
|
|
/* save image */
|
2019-12-09 06:03:23 +00:00
|
|
|
if (! print (output, error, "static unsigned char header_data[] = {\n\t"))
|
2014-10-03 23:40:13 +02:00
|
|
|
goto fail;
|
1999-06-28 17:54:19 +00:00
|
|
|
|
2012-11-19 00:34:36 +01:00
|
|
|
data = g_new (guchar, width * 1);
|
1999-06-28 17:54:19 +00:00
|
|
|
|
|
|
|
c = 0;
|
2012-11-19 00:34:36 +01:00
|
|
|
for (y = 0; y < height; y++)
|
1999-06-28 17:54:19 +00:00
|
|
|
{
|
2012-11-19 00:34:36 +01:00
|
|
|
gegl_buffer_get (buffer, GEGL_RECTANGLE (0, y, width, 1), 1.0,
|
|
|
|
NULL, data,
|
|
|
|
GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
|
2008-09-17 08:37:24 +00:00
|
|
|
|
2012-11-19 00:34:36 +01:00
|
|
|
for (x = 0; x < width -1; x++)
|
2008-10-20 06:04:39 +00:00
|
|
|
{
|
2012-11-19 00:34:36 +01:00
|
|
|
d = data + x * 1;
|
1999-06-28 17:54:19 +00:00
|
|
|
|
2014-10-04 02:30:37 +02:00
|
|
|
if (! print (output, error, "%d,", (gint) d[0]))
|
2014-10-03 23:40:13 +02:00
|
|
|
goto fail;
|
1999-06-28 17:54:19 +00:00
|
|
|
|
2008-10-20 06:04:39 +00:00
|
|
|
c++;
|
|
|
|
if (c >= 16)
|
|
|
|
{
|
2014-10-04 02:30:37 +02:00
|
|
|
if (! print (output, error, "\n\t"))
|
2014-10-03 23:40:13 +02:00
|
|
|
goto fail;
|
|
|
|
|
2008-10-20 06:04:39 +00:00
|
|
|
c = 0;
|
|
|
|
}
|
|
|
|
}
|
1999-06-28 17:54:19 +00:00
|
|
|
|
2012-11-19 00:34:36 +01:00
|
|
|
if (y != height - 1)
|
2014-10-03 23:40:13 +02:00
|
|
|
{
|
2014-10-04 02:30:37 +02:00
|
|
|
if (! print (output, error, "%d,\n\t", (gint) d[1]))
|
2014-10-03 23:40:13 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
1999-06-28 17:54:19 +00:00
|
|
|
else
|
2014-10-03 23:40:13 +02:00
|
|
|
{
|
2014-10-04 02:30:37 +02:00
|
|
|
if (! print (output, error, "%d\n\t", (gint) d[1]))
|
2014-10-03 23:40:13 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2008-09-17 08:37:24 +00:00
|
|
|
|
1999-06-28 17:54:19 +00:00
|
|
|
c = 0; /* reset line counter */
|
|
|
|
}
|
2014-10-03 23:40:13 +02:00
|
|
|
|
2014-10-04 02:30:37 +02:00
|
|
|
if (! print (output, error, "};\n"))
|
2014-10-03 23:40:13 +02:00
|
|
|
goto fail;
|
1999-06-28 17:54:19 +00:00
|
|
|
break;
|
2008-09-17 08:37:24 +00:00
|
|
|
|
1999-07-06 17:26:06 +00:00
|
|
|
default:
|
|
|
|
g_warning ("unhandled drawable type (%d)", drawable_type);
|
2014-10-03 23:40:13 +02:00
|
|
|
goto fail;
|
2008-09-17 08:37:24 +00:00
|
|
|
}
|
1997-11-24 22:05:25 +00:00
|
|
|
|
2014-10-03 23:40:13 +02:00
|
|
|
if (! g_output_stream_close (output, NULL, error))
|
|
|
|
goto fail;
|
|
|
|
|
1997-11-24 22:05:25 +00:00
|
|
|
g_free (data);
|
2014-10-03 23:40:13 +02:00
|
|
|
g_object_unref (output);
|
2012-11-19 00:34:36 +01:00
|
|
|
g_object_unref (buffer);
|
1997-11-24 22:05:25 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2014-10-03 23:40:13 +02:00
|
|
|
|
|
|
|
fail:
|
|
|
|
|
2018-11-27 12:27:20 +01:00
|
|
|
cancellable = g_cancellable_new ();
|
|
|
|
g_cancellable_cancel (cancellable);
|
|
|
|
g_output_stream_close (output, cancellable, NULL);
|
|
|
|
|
2014-10-03 23:40:13 +02:00
|
|
|
g_free (data);
|
|
|
|
g_object_unref (output);
|
|
|
|
g_object_unref (buffer);
|
2018-11-27 12:27:20 +01:00
|
|
|
g_object_unref (cancellable);
|
2014-10-03 23:40:13 +02:00
|
|
|
|
|
|
|
return FALSE;
|
1997-11-24 22:05:25 +00:00
|
|
|
}
|
2015-10-17 23:28:45 +02:00
|
|
|
|
|
|
|
static gboolean
|
|
|
|
print (GOutputStream *output,
|
|
|
|
GError **error,
|
|
|
|
const gchar *format,
|
|
|
|
...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
gboolean success;
|
|
|
|
|
|
|
|
va_start (args, format);
|
|
|
|
success = g_output_stream_vprintf (output, NULL, NULL,
|
|
|
|
error, format, args);
|
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|