2006-12-10 06:12:23 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
2005-06-01 03:10:46 +08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2006-12-10 06:12:23 +08:00
|
|
|
* cel.c -- KISS CEL file format plug-in
|
|
|
|
* (copyright) 1997,1998 Nick Lamb (njl195@zepler.org.uk)
|
|
|
|
*
|
2009-01-18 06:28:01 +08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
2005-06-01 03:10:46 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
2009-01-18 06:28:01 +08:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2005-06-01 03:10:46 +08:00
|
|
|
* (at your option) any later version.
|
1998-04-26 17:35:56 +08:00
|
|
|
*
|
2005-06-01 03:10:46 +08:00
|
|
|
* 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
|
2009-01-18 06:28:01 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
1997-11-25 06:05:25 +08:00
|
|
|
*/
|
2005-03-04 23:12:29 +08:00
|
|
|
|
1999-05-29 09:28:24 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
2003-06-13 22:37:00 +08:00
|
|
|
#include <errno.h>
|
1997-11-25 06:05:25 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2005-03-04 23:12:29 +08:00
|
|
|
#include <glib/gstdio.h>
|
2000-01-07 00:40:17 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
#include <libgimp/gimp.h>
|
2000-01-07 00:40:17 +08:00
|
|
|
#include <libgimp/gimpui.h>
|
2000-01-18 01:02:26 +08:00
|
|
|
|
1999-12-18 05:24:24 +08:00
|
|
|
#include "libgimp/stdplugins-intl.h"
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2004-05-15 00:54:33 +08:00
|
|
|
|
2005-08-14 02:29:14 +08:00
|
|
|
#define LOAD_PROC "file-cel-load"
|
|
|
|
#define SAVE_PROC "file-cel-save"
|
2008-08-11 18:06:13 +08:00
|
|
|
#define PLUG_IN_BINARY "file-cel"
|
2005-08-14 02:29:14 +08:00
|
|
|
|
|
|
|
|
2000-01-18 01:02:26 +08:00
|
|
|
static void query (void);
|
2003-07-02 02:54:28 +08:00
|
|
|
static void run (const gchar *name,
|
2003-12-24 06:07:06 +08:00
|
|
|
gint nparams,
|
|
|
|
const GimpParam *param,
|
|
|
|
gint *nreturn_vals,
|
|
|
|
GimpParam **return_vals);
|
2000-01-18 01:02:26 +08:00
|
|
|
|
2008-08-20 22:00:44 +08:00
|
|
|
static gint load_palette (FILE *fp,
|
|
|
|
guchar palette[]);
|
|
|
|
static gint32 load_image (const gchar *file,
|
|
|
|
const gchar *brief,
|
|
|
|
GError **error);
|
|
|
|
static gboolean save_image (const gchar *file,
|
|
|
|
const gchar *brief,
|
|
|
|
gint32 image,
|
|
|
|
gint32 layer,
|
|
|
|
GError **error);
|
|
|
|
static void palette_dialog (const gchar *title);
|
|
|
|
static gboolean need_palette (const gchar *file);
|
2004-01-06 01:37:42 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* Globals... */
|
|
|
|
|
2006-05-16 20:26:20 +08:00
|
|
|
const GimpPlugInInfo PLUG_IN_INFO =
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-01-18 01:02:26 +08:00
|
|
|
NULL, /* init_proc */
|
|
|
|
NULL, /* quit_proc */
|
|
|
|
query, /* query_proc */
|
|
|
|
run, /* run_proc */
|
1997-11-25 06:05:25 +08:00
|
|
|
};
|
|
|
|
|
2004-01-06 02:54:30 +08:00
|
|
|
static gchar *palette_file = NULL;
|
|
|
|
static gsize data_length = 0;
|
1998-04-26 17:35:56 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* Let GIMP library handle initialisation (and inquisitive users) */
|
|
|
|
|
2000-01-18 01:02:26 +08:00
|
|
|
MAIN ()
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
/* GIMP queries plug-in for parameters etc. */
|
|
|
|
|
2000-01-18 01:02:26 +08:00
|
|
|
static void
|
|
|
|
query (void)
|
|
|
|
{
|
2006-05-16 20:26:20 +08:00
|
|
|
static const GimpParamDef load_args[] =
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2009-01-20 04:11:36 +08:00
|
|
|
{ GIMP_PDB_INT32, "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
|
2005-08-14 02:29:14 +08:00
|
|
|
{ GIMP_PDB_STRING, "filename", "Filename to load image from" },
|
|
|
|
{ GIMP_PDB_STRING, "raw-filename", "Name entered" },
|
|
|
|
{ GIMP_PDB_STRING, "palette-filename", "Filename to load palette from" }
|
1997-11-25 06:05:25 +08:00
|
|
|
};
|
2006-05-16 20:26:20 +08:00
|
|
|
static const GimpParamDef load_return_vals[] =
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2000-08-22 09:26:57 +08:00
|
|
|
{ GIMP_PDB_IMAGE, "image", "Output image" }
|
1997-11-25 06:05:25 +08:00
|
|
|
};
|
2000-01-26 01:46:56 +08:00
|
|
|
|
2006-05-16 20:26:20 +08:00
|
|
|
static const GimpParamDef save_args[] =
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
2009-01-20 04:11:36 +08:00
|
|
|
{ GIMP_PDB_INT32, "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
|
2005-08-14 02:29:14 +08:00
|
|
|
{ GIMP_PDB_IMAGE, "image", "Input image" },
|
|
|
|
{ GIMP_PDB_DRAWABLE, "drawable", "Drawable to save" },
|
|
|
|
{ GIMP_PDB_STRING, "filename", "Filename to save image to" },
|
|
|
|
{ GIMP_PDB_STRING, "raw-filename", "Name entered" },
|
|
|
|
{ GIMP_PDB_STRING, "palette-filename", "Filename to save palette to" },
|
1997-11-25 06:05:25 +08:00
|
|
|
};
|
|
|
|
|
2005-08-14 02:29:14 +08:00
|
|
|
gimp_install_procedure (LOAD_PROC,
|
2003-12-24 06:07:06 +08:00
|
|
|
"Loads files in KISS CEL file format",
|
|
|
|
"This plug-in loads individual KISS cell files.",
|
|
|
|
"Nick Lamb",
|
|
|
|
"Nick Lamb <njl195@zepler.org.uk>",
|
|
|
|
"May 1998",
|
2004-05-15 00:54:33 +08:00
|
|
|
N_("KISS CEL"),
|
2003-12-24 06:07:06 +08:00
|
|
|
NULL,
|
|
|
|
GIMP_PLUGIN,
|
|
|
|
G_N_ELEMENTS (load_args),
|
2001-12-06 10:28:58 +08:00
|
|
|
G_N_ELEMENTS (load_return_vals),
|
2003-12-24 06:07:06 +08:00
|
|
|
load_args, load_return_vals);
|
2000-01-18 01:02:26 +08:00
|
|
|
|
2005-08-14 02:29:14 +08:00
|
|
|
gimp_register_magic_load_handler (LOAD_PROC,
|
2004-05-15 00:54:33 +08:00
|
|
|
"cel",
|
|
|
|
"",
|
|
|
|
"0,string,KiSS\\040");
|
|
|
|
|
2005-08-14 02:29:14 +08:00
|
|
|
gimp_install_procedure (SAVE_PROC,
|
2003-12-24 06:07:06 +08:00
|
|
|
"Saves files in KISS CEL file format",
|
|
|
|
"This plug-in saves individual KISS cell files.",
|
|
|
|
"Nick Lamb",
|
|
|
|
"Nick Lamb <njl195@zepler.org.uk>",
|
|
|
|
"May 1998",
|
2004-05-15 00:54:33 +08:00
|
|
|
N_("KISS CEL"),
|
2008-10-20 14:04:39 +08:00
|
|
|
"RGB*, INDEXED*",
|
2003-12-24 06:07:06 +08:00
|
|
|
GIMP_PLUGIN,
|
|
|
|
G_N_ELEMENTS (save_args), 0,
|
|
|
|
save_args, NULL);
|
2000-01-18 01:02:26 +08:00
|
|
|
|
2005-08-14 02:29:14 +08:00
|
|
|
gimp_register_save_handler (SAVE_PROC, "cel", "");
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2000-01-18 01:02:26 +08:00
|
|
|
static void
|
2003-07-02 02:54:28 +08:00
|
|
|
run (const gchar *name,
|
|
|
|
gint nparams,
|
|
|
|
const GimpParam *param,
|
|
|
|
gint *nreturn_vals,
|
|
|
|
GimpParam **return_vals)
|
2000-01-18 01:02:26 +08:00
|
|
|
{
|
2003-07-02 02:54:28 +08:00
|
|
|
static GimpParam values[2]; /* Return values */
|
|
|
|
GimpRunMode run_mode;
|
2004-01-12 04:10:14 +08:00
|
|
|
gint32 image_ID;
|
|
|
|
gint32 drawable_ID;
|
2003-07-02 02:54:28 +08:00
|
|
|
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
|
|
|
|
gint32 image;
|
2004-01-12 04:10:14 +08:00
|
|
|
GimpExportReturn export = GIMP_EXPORT_CANCEL;
|
2008-08-20 22:00:44 +08:00
|
|
|
GError *error = NULL;
|
1998-04-26 17:35:56 +08:00
|
|
|
|
|
|
|
run_mode = param[0].data.d_int32;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-03-26 00:38:19 +08:00
|
|
|
INIT_I18N ();
|
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
/* Set up default return values */
|
|
|
|
|
1998-04-26 17:35:56 +08:00
|
|
|
*nreturn_vals = 1;
|
1997-11-25 06:05:25 +08:00
|
|
|
*return_vals = values;
|
2000-08-22 09:26:57 +08:00
|
|
|
values[0].type = GIMP_PDB_STATUS;
|
|
|
|
values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2005-08-14 02:29:14 +08:00
|
|
|
if (strcmp (name, LOAD_PROC) == 0)
|
2000-01-18 01:02:26 +08:00
|
|
|
{
|
2000-08-22 09:26:57 +08:00
|
|
|
if (run_mode != GIMP_RUN_NONINTERACTIVE)
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
2005-08-14 02:29:14 +08:00
|
|
|
data_length = gimp_get_data_size (SAVE_PROC);
|
2003-12-24 06:07:06 +08:00
|
|
|
if (data_length > 0)
|
|
|
|
{
|
2004-01-06 02:54:30 +08:00
|
|
|
palette_file = g_malloc (data_length);
|
2005-08-14 02:29:14 +08:00
|
|
|
gimp_get_data (SAVE_PROC, palette_file);
|
2003-12-24 06:07:06 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-01-06 02:54:30 +08:00
|
|
|
palette_file = g_strdup ("*.kcf");
|
|
|
|
data_length = strlen (palette_file) + 1;
|
2003-12-24 06:07:06 +08:00
|
|
|
}
|
|
|
|
}
|
2000-01-18 01:02:26 +08:00
|
|
|
|
2000-08-22 09:26:57 +08:00
|
|
|
if (run_mode == GIMP_RUN_NONINTERACTIVE)
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
|
|
|
palette_file = param[3].data.d_string;
|
2004-01-06 02:54:30 +08:00
|
|
|
data_length = strlen (palette_file) + 1;
|
2003-12-24 06:07:06 +08:00
|
|
|
}
|
2000-08-22 09:26:57 +08:00
|
|
|
else if (run_mode == GIMP_RUN_INTERACTIVE)
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
|
|
|
/* Let user choose KCF palette (cancel ignores) */
|
2004-01-06 02:54:30 +08:00
|
|
|
if (need_palette (param[1].data.d_string))
|
2004-01-06 01:37:42 +08:00
|
|
|
palette_dialog (_("Load KISS Palette"));
|
2004-01-06 02:54:30 +08:00
|
|
|
|
2005-08-14 02:29:14 +08:00
|
|
|
gimp_set_data (SAVE_PROC, palette_file, data_length);
|
2003-12-24 06:07:06 +08:00
|
|
|
}
|
2000-01-18 01:02:26 +08:00
|
|
|
|
2008-08-20 22:00:44 +08:00
|
|
|
image = load_image (param[1].data.d_string, param[2].data.d_string,
|
|
|
|
&error);
|
2000-01-18 01:02:26 +08:00
|
|
|
|
|
|
|
if (image != -1)
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
|
|
|
*nreturn_vals = 2;
|
|
|
|
values[1].type = GIMP_PDB_IMAGE;
|
|
|
|
values[1].data.d_image = image;
|
|
|
|
}
|
2000-01-18 01:02:26 +08:00
|
|
|
else
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
|
|
|
status = GIMP_PDB_EXECUTION_ERROR;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
2005-08-14 02:29:14 +08:00
|
|
|
else if (strcmp (name, SAVE_PROC) == 0)
|
2000-01-18 01:02:26 +08:00
|
|
|
{
|
2004-01-12 04:10:14 +08:00
|
|
|
image_ID = param[1].data.d_int32;
|
|
|
|
drawable_ID = param[2].data.d_int32;
|
2004-01-21 01:10:16 +08:00
|
|
|
|
2004-01-12 04:10:14 +08:00
|
|
|
/* eventually export the image */
|
|
|
|
switch (run_mode)
|
2008-10-20 14:04:39 +08:00
|
|
|
{
|
|
|
|
case GIMP_RUN_INTERACTIVE:
|
|
|
|
case GIMP_RUN_WITH_LAST_VALS:
|
|
|
|
gimp_ui_init (PLUG_IN_BINARY, FALSE);
|
2009-07-21 20:10:29 +08:00
|
|
|
export = gimp_export_image (&image_ID, &drawable_ID, NULL,
|
2008-10-20 14:04:39 +08:00
|
|
|
(GIMP_EXPORT_CAN_HANDLE_RGB |
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_ALPHA |
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_INDEXED
|
|
|
|
));
|
|
|
|
if (export == GIMP_EXPORT_CANCEL)
|
|
|
|
{
|
|
|
|
values[0].data.d_status = GIMP_PDB_CANCEL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2004-01-12 04:10:14 +08:00
|
|
|
|
|
|
|
if (save_image (param[3].data.d_string, param[4].data.d_string,
|
2008-10-20 14:04:39 +08:00
|
|
|
image_ID, drawable_ID, &error))
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
2005-08-14 02:29:14 +08:00
|
|
|
gimp_set_data (SAVE_PROC, palette_file, data_length);
|
2003-12-24 06:07:06 +08:00
|
|
|
}
|
2000-01-18 01:02:26 +08:00
|
|
|
else
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
2004-01-12 04:10:14 +08:00
|
|
|
status = GIMP_PDB_EXECUTION_ERROR;
|
2003-12-24 06:07:06 +08:00
|
|
|
}
|
2004-01-12 04:10:14 +08:00
|
|
|
|
|
|
|
if (export == GIMP_EXPORT_EXPORT)
|
2008-10-20 14:04:39 +08:00
|
|
|
gimp_image_delete (image_ID);
|
2000-01-18 01:02:26 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-08-22 09:26:57 +08:00
|
|
|
status = GIMP_PDB_CALLING_ERROR;
|
1998-04-26 17:35:56 +08:00
|
|
|
}
|
2000-01-26 01:46:56 +08:00
|
|
|
|
2008-08-20 22:00:44 +08:00
|
|
|
if (status != GIMP_PDB_SUCCESS && error)
|
|
|
|
{
|
|
|
|
*nreturn_vals = 2;
|
|
|
|
values[1].type = GIMP_PDB_STRING;
|
|
|
|
values[1].data.d_string = error->message;
|
|
|
|
}
|
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
values[0].data.d_status = status;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2004-01-06 01:37:42 +08:00
|
|
|
/* Peek into the file to determine whether we need a palette */
|
|
|
|
static gboolean
|
2004-01-06 02:54:30 +08:00
|
|
|
need_palette (const gchar *file)
|
2004-01-06 01:37:42 +08:00
|
|
|
{
|
2004-01-06 05:32:25 +08:00
|
|
|
FILE *fp;
|
2004-01-06 03:01:33 +08:00
|
|
|
guchar header[32];
|
|
|
|
|
2005-03-05 02:31:43 +08:00
|
|
|
fp = g_fopen (file, "rb");
|
2004-01-06 03:01:33 +08:00
|
|
|
if (!fp)
|
|
|
|
return FALSE;
|
2004-01-06 01:37:42 +08:00
|
|
|
|
2004-01-06 02:54:30 +08:00
|
|
|
fread (header, 32, 1, fp);
|
|
|
|
fclose (fp);
|
2004-01-06 01:37:42 +08:00
|
|
|
|
2004-01-06 03:01:33 +08:00
|
|
|
return (header[5] < 32);
|
2004-01-06 01:37:42 +08:00
|
|
|
}
|
|
|
|
|
2006-12-10 06:12:23 +08:00
|
|
|
/* Load CEL image into GIMP */
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-01-18 01:02:26 +08:00
|
|
|
static gint32
|
2008-08-20 22:00:44 +08:00
|
|
|
load_image (const gchar *file,
|
|
|
|
const gchar *brief,
|
|
|
|
GError **error)
|
2000-01-18 01:02:26 +08:00
|
|
|
{
|
2000-01-26 01:46:56 +08:00
|
|
|
FILE *fp; /* Read file pointer */
|
|
|
|
guchar header[32]; /* File header */
|
|
|
|
gint height, width, /* Dimensions of image */
|
|
|
|
offx, offy, /* Layer offets */
|
2004-01-06 01:37:42 +08:00
|
|
|
colours, /* Number of colours */
|
|
|
|
bpp; /* Bits per pixel */
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
gint32 image, /* Image */
|
|
|
|
layer; /* Layer */
|
|
|
|
guchar *palette, /* 24 bit palette */
|
|
|
|
*buffer, /* Temporary buffer */
|
|
|
|
*line; /* Pixel data */
|
2004-01-06 02:54:30 +08:00
|
|
|
GimpDrawable *drawable; /* Drawable for layer */
|
|
|
|
GimpPixelRgn pixel_rgn; /* Pixel region for layer */
|
2000-01-26 01:46:56 +08:00
|
|
|
|
|
|
|
gint i, j, k; /* Counters */
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
|
2000-01-18 01:02:26 +08:00
|
|
|
/* Open the file for reading */
|
2005-03-04 23:12:29 +08:00
|
|
|
fp = g_fopen (file, "r");
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-01-18 01:02:26 +08:00
|
|
|
if (fp == NULL)
|
|
|
|
{
|
2008-08-20 22:00:44 +08:00
|
|
|
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
|
|
|
|
_("Could not open '%s' for reading: %s"),
|
|
|
|
gimp_filename_to_utf8 (file), g_strerror (errno));
|
2000-01-26 01:46:56 +08:00
|
|
|
return -1;
|
2000-01-18 01:02:26 +08:00
|
|
|
}
|
1998-04-26 17:35:56 +08:00
|
|
|
|
2005-09-30 16:16:10 +08:00
|
|
|
gimp_progress_init_printf (_("Opening '%s'"),
|
|
|
|
gimp_filename_to_utf8 (brief));
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-01-18 01:02:26 +08:00
|
|
|
/* Get the image dimensions and create the image... */
|
|
|
|
|
|
|
|
fread (header, 4, 1, fp);
|
|
|
|
|
2005-08-03 09:15:36 +08:00
|
|
|
if (strncmp ((const gchar *) header, "KiSS", 4))
|
2000-01-18 01:02:26 +08:00
|
|
|
{
|
|
|
|
colours= 16;
|
2004-01-06 01:37:42 +08:00
|
|
|
bpp = 4;
|
2005-08-03 09:15:36 +08:00
|
|
|
width = header[0] + (256 * header[1]);
|
|
|
|
height = header[2] + (256 * header[3]);
|
2000-01-18 01:02:26 +08:00
|
|
|
offx= 0;
|
|
|
|
offy= 0;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
2000-01-18 01:02:26 +08:00
|
|
|
else
|
|
|
|
{ /* New-style image file, read full header */
|
|
|
|
fread (header, 28, 1, fp);
|
2004-01-06 01:37:42 +08:00
|
|
|
bpp = header[1];
|
|
|
|
if (bpp == 24)
|
|
|
|
colours = -1;
|
|
|
|
else
|
|
|
|
colours = (1 << header[1]);
|
2000-01-18 01:02:26 +08:00
|
|
|
width = header[4] + (256 * header[5]);
|
|
|
|
height = header[6] + (256 * header[7]);
|
|
|
|
offx = header[8] + (256 * header[9]);
|
|
|
|
offy = header[10] + (256 * header[11]);
|
|
|
|
}
|
|
|
|
|
2004-01-06 01:37:42 +08:00
|
|
|
if (bpp == 32)
|
|
|
|
image = gimp_image_new (width + offx, height + offy, GIMP_RGB);
|
|
|
|
else
|
|
|
|
image = gimp_image_new (width + offx, height + offy, GIMP_INDEXED);
|
2000-01-18 01:02:26 +08:00
|
|
|
|
|
|
|
if (image == -1)
|
|
|
|
{
|
2003-06-13 22:37:00 +08:00
|
|
|
g_message (_("Can't create a new image"));
|
2000-01-26 01:46:56 +08:00
|
|
|
return -1;
|
2000-01-18 01:02:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
gimp_image_set_filename (image, file);
|
|
|
|
|
|
|
|
/* Create an indexed-alpha layer to hold the image... */
|
2004-01-06 01:37:42 +08:00
|
|
|
if (bpp == 32)
|
|
|
|
layer = gimp_layer_new (image, _("Background"), width, height,
|
|
|
|
GIMP_RGBA_IMAGE, 100, GIMP_NORMAL_MODE);
|
|
|
|
else
|
|
|
|
layer = gimp_layer_new (image, _("Background"), width, height,
|
|
|
|
GIMP_INDEXEDA_IMAGE, 100, GIMP_NORMAL_MODE);
|
2000-01-18 01:02:26 +08:00
|
|
|
gimp_image_add_layer (image, layer, 0);
|
|
|
|
gimp_layer_set_offsets (layer, offx, offy);
|
|
|
|
|
|
|
|
/* Get the drawable and set the pixel region for our load... */
|
|
|
|
|
|
|
|
drawable = gimp_drawable_get (layer);
|
|
|
|
|
|
|
|
gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0, drawable->width,
|
2003-12-24 06:07:06 +08:00
|
|
|
drawable->height, TRUE, FALSE);
|
2000-01-18 01:02:26 +08:00
|
|
|
|
2007-06-06 16:44:52 +08:00
|
|
|
/* Read the image in and give it to GIMP a line at a time */
|
2004-01-06 02:54:30 +08:00
|
|
|
buffer = g_new (guchar, width * 4);
|
|
|
|
line = g_new (guchar, (width + 1) * 4);
|
2000-01-18 01:02:26 +08:00
|
|
|
|
|
|
|
for (i = 0; i < height && !feof(fp); ++i)
|
|
|
|
{
|
2004-01-06 01:37:42 +08:00
|
|
|
switch (bpp)
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
2004-01-06 01:37:42 +08:00
|
|
|
case 4:
|
2003-12-24 06:07:06 +08:00
|
|
|
fread (buffer, (width+1)/2, 1, fp);
|
|
|
|
for (j = 0, k = 0; j < width*2; j+= 4, ++k)
|
|
|
|
{
|
|
|
|
if (buffer[k] / 16 == 0)
|
|
|
|
{
|
|
|
|
line[j]= 16;
|
|
|
|
line[j+1]= 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
line[j]= (buffer[k] / 16) - 1;
|
|
|
|
line[j+1]= 255;
|
|
|
|
}
|
|
|
|
if (buffer[k] % 16 == 0)
|
|
|
|
{
|
|
|
|
line[j+2]= 16;
|
|
|
|
line[j+3]= 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
line[j+2]= (buffer[k] % 16) - 1;
|
|
|
|
line[j+3]= 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2004-01-06 01:37:42 +08:00
|
|
|
case 8:
|
2003-12-24 06:07:06 +08:00
|
|
|
fread (buffer, width, 1, fp);
|
|
|
|
for (j = 0, k = 0; j < width*2; j+= 2, ++k)
|
|
|
|
{
|
|
|
|
if (buffer[k] == 0)
|
|
|
|
{
|
|
|
|
line[j]= 255;
|
|
|
|
line[j+1]= 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
line[j]= buffer[k] - 1;
|
|
|
|
line[j+1]= 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2004-01-06 02:54:30 +08:00
|
|
|
|
2004-01-06 01:37:42 +08:00
|
|
|
case 32:
|
|
|
|
fread (line, width*4, 1, fp);
|
|
|
|
/* The CEL file order is BGR so we need to swap B and R
|
|
|
|
* to get the Gimp RGB order.
|
|
|
|
*/
|
|
|
|
for (j= 0; j < width; j++)
|
|
|
|
{
|
|
|
|
guint8 tmp = line[j*4];
|
|
|
|
line[j*4] = line[j*4+2];
|
|
|
|
line[j*4+2] = tmp;
|
|
|
|
}
|
|
|
|
break;
|
2003-12-24 06:07:06 +08:00
|
|
|
|
|
|
|
default:
|
2004-01-06 01:37:42 +08:00
|
|
|
g_message (_("Unsupported bit depth (%d)!"), bpp);
|
2003-12-24 06:07:06 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2003-11-15 21:53:33 +08:00
|
|
|
|
2000-01-18 01:02:26 +08:00
|
|
|
gimp_pixel_rgn_set_rect (&pixel_rgn, line, 0, i, drawable->width, 1);
|
|
|
|
gimp_progress_update ((float) i / (float) height);
|
1998-04-26 17:35:56 +08:00
|
|
|
}
|
|
|
|
|
2000-01-18 01:02:26 +08:00
|
|
|
/* Close image files, give back allocated memory */
|
1998-04-26 17:35:56 +08:00
|
|
|
|
2000-01-18 01:02:26 +08:00
|
|
|
fclose (fp);
|
|
|
|
g_free (buffer);
|
|
|
|
g_free (line);
|
1998-04-26 17:35:56 +08:00
|
|
|
|
2004-01-06 01:37:42 +08:00
|
|
|
if (bpp != 32)
|
2000-01-18 01:02:26 +08:00
|
|
|
{
|
2004-01-06 01:37:42 +08:00
|
|
|
/* Use palette from file or otherwise default grey palette */
|
|
|
|
palette = g_new (guchar, colours*3);
|
2004-01-06 02:54:30 +08:00
|
|
|
|
2004-01-06 01:37:42 +08:00
|
|
|
/* Open the file for reading if user picked one */
|
|
|
|
if (palette_file == NULL)
|
|
|
|
{
|
|
|
|
fp = NULL;
|
|
|
|
}
|
|
|
|
else
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
2005-03-05 02:31:43 +08:00
|
|
|
fp = g_fopen (palette_file, "r");
|
2003-12-24 06:07:06 +08:00
|
|
|
}
|
2004-01-06 02:54:30 +08:00
|
|
|
|
2004-01-06 01:37:42 +08:00
|
|
|
if (fp != NULL)
|
|
|
|
{
|
|
|
|
colours = load_palette (fp, palette);
|
|
|
|
fclose (fp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i= 0; i < colours; ++i)
|
|
|
|
{
|
|
|
|
palette[i*3] = palette[i*3+1] = palette[i*3+2]= i * 256 / colours;
|
|
|
|
}
|
|
|
|
}
|
2004-01-06 02:54:30 +08:00
|
|
|
|
2004-11-02 20:00:25 +08:00
|
|
|
gimp_image_set_colormap (image, palette + 3, colours - 1);
|
2004-01-06 02:54:30 +08:00
|
|
|
|
2004-01-06 01:37:42 +08:00
|
|
|
/* Close palette file, give back allocated memory */
|
2004-01-06 02:54:30 +08:00
|
|
|
|
2004-01-06 01:37:42 +08:00
|
|
|
g_free (palette);
|
2000-01-18 01:02:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now get everything redrawn and hand back the finished image */
|
|
|
|
|
|
|
|
gimp_drawable_flush (drawable);
|
|
|
|
gimp_drawable_detach (drawable);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2008-08-20 22:00:44 +08:00
|
|
|
return image;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2000-01-18 01:02:26 +08:00
|
|
|
static gint
|
|
|
|
load_palette (FILE *fp,
|
2003-12-24 06:07:06 +08:00
|
|
|
guchar palette[])
|
2000-01-18 01:02:26 +08:00
|
|
|
{
|
2003-12-24 06:07:06 +08:00
|
|
|
guchar header[32]; /* File header */
|
|
|
|
guchar buffer[2];
|
|
|
|
int i, bpp, colours= 0;
|
1998-04-26 17:35:56 +08:00
|
|
|
|
2000-01-18 01:02:26 +08:00
|
|
|
fread (header, 4, 1, fp);
|
2005-08-03 09:15:36 +08:00
|
|
|
if (!strncmp ((const gchar *) header, "KiSS", 4))
|
2000-01-18 01:02:26 +08:00
|
|
|
{
|
|
|
|
fread (header+4, 28, 1, fp);
|
|
|
|
bpp = header[5];
|
|
|
|
colours = header[8] + header[9] * 256;
|
|
|
|
if (bpp == 12)
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
2005-08-03 09:15:36 +08:00
|
|
|
for (i = 0; i < colours; ++i)
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
|
|
|
fread (buffer, 1, 2, fp);
|
|
|
|
palette[i*3]= buffer[0] & 0xf0;
|
|
|
|
palette[i*3+1]= (buffer[1] & 0x0f) * 16;
|
|
|
|
palette[i*3+2]= (buffer[0] & 0x0f) * 16;
|
|
|
|
}
|
|
|
|
}
|
2000-01-18 01:02:26 +08:00
|
|
|
else
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
|
|
|
fread (palette, colours, 3, fp);
|
|
|
|
}
|
1998-04-26 17:35:56 +08:00
|
|
|
}
|
2000-01-18 01:02:26 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
colours = 16; bpp = 12;
|
|
|
|
fseek (fp, 0, SEEK_SET);
|
|
|
|
for (i= 0; i < colours; ++i)
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
|
|
|
fread (buffer, 1, 2, fp);
|
|
|
|
palette[i*3] = buffer[0] & 0xf0;
|
|
|
|
palette[i*3+1] = (buffer[1] & 0x0f) * 16;
|
|
|
|
palette[i*3+2] = (buffer[0] & 0x0f) * 16;
|
|
|
|
}
|
1998-04-26 17:35:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return colours;
|
|
|
|
}
|
|
|
|
|
2005-06-01 03:10:46 +08:00
|
|
|
static gboolean
|
2008-08-20 22:00:44 +08:00
|
|
|
save_image (const gchar *file,
|
|
|
|
const gchar *brief,
|
|
|
|
gint32 image,
|
|
|
|
gint32 layer,
|
|
|
|
GError **error)
|
2000-01-18 01:02:26 +08:00
|
|
|
{
|
2003-06-13 22:37:00 +08:00
|
|
|
FILE *fp; /* Write file pointer */
|
|
|
|
guchar header[32]; /* File header */
|
2004-01-06 01:37:42 +08:00
|
|
|
gint bpp; /* Bit per pixel */
|
2003-06-13 22:37:00 +08:00
|
|
|
gint colours, type; /* Number of colours, type of layer */
|
|
|
|
gint offx, offy; /* Layer offsets */
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-06-13 22:37:00 +08:00
|
|
|
guchar *buffer; /* Temporary buffer */
|
|
|
|
guchar *line; /* Pixel data */
|
|
|
|
GimpDrawable *drawable; /* Drawable for layer */
|
|
|
|
GimpPixelRgn pixel_rgn; /* Pixel region for layer */
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-06-13 22:37:00 +08:00
|
|
|
gint i, j, k; /* Counters */
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-01-18 01:02:26 +08:00
|
|
|
/* Check that this is an indexed image, fail otherwise */
|
|
|
|
type = gimp_drawable_type (layer);
|
2000-08-22 09:26:57 +08:00
|
|
|
if (type != GIMP_INDEXEDA_IMAGE)
|
2004-01-06 01:37:42 +08:00
|
|
|
bpp = 32;
|
|
|
|
else
|
|
|
|
bpp = 4;
|
2004-01-06 02:54:30 +08:00
|
|
|
|
1998-05-30 15:32:37 +08:00
|
|
|
/* Find out how offset this layer was */
|
2000-01-18 01:02:26 +08:00
|
|
|
gimp_drawable_offsets (layer, &offx, &offy);
|
1998-05-30 15:32:37 +08:00
|
|
|
|
2000-01-18 01:02:26 +08:00
|
|
|
drawable = gimp_drawable_get (layer);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-01-18 01:02:26 +08:00
|
|
|
/* Open the file for writing */
|
2005-03-04 23:12:29 +08:00
|
|
|
fp = g_fopen (file, "w");
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-01-18 01:02:26 +08:00
|
|
|
if (fp == NULL)
|
|
|
|
{
|
2008-08-20 22:00:44 +08:00
|
|
|
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
|
|
|
|
_("Could not open '%s' for writing: %s"),
|
|
|
|
gimp_filename_to_utf8 (file), g_strerror (errno));
|
2000-01-26 01:46:56 +08:00
|
|
|
return FALSE;
|
2000-01-18 01:02:26 +08:00
|
|
|
}
|
1998-05-30 15:32:37 +08:00
|
|
|
|
2005-09-30 16:16:10 +08:00
|
|
|
gimp_progress_init_printf (_("Saving '%s'"),
|
|
|
|
gimp_filename_to_utf8 (brief));
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-01-18 01:02:26 +08:00
|
|
|
/* Headers */
|
|
|
|
memset (header, 0, 32);
|
2005-08-03 09:15:36 +08:00
|
|
|
strcpy ((gchar *) header, "KiSS");
|
1997-11-25 06:05:25 +08:00
|
|
|
header[4]= 0x20;
|
|
|
|
|
2000-01-18 01:02:26 +08:00
|
|
|
/* Work out whether to save as 8bit or 4bit */
|
2004-01-06 01:37:42 +08:00
|
|
|
if (bpp < 32)
|
2000-01-18 01:02:26 +08:00
|
|
|
{
|
2004-11-02 20:00:25 +08:00
|
|
|
g_free (gimp_image_get_colormap (image, &colours));
|
2004-01-06 01:37:42 +08:00
|
|
|
if (colours > 15)
|
|
|
|
{
|
|
|
|
header[5] = 8;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
header[5] = 4;
|
|
|
|
}
|
2000-01-18 01:02:26 +08:00
|
|
|
}
|
|
|
|
else
|
2004-01-06 01:37:42 +08:00
|
|
|
header[5] = 32;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-01-18 01:02:26 +08:00
|
|
|
/* Fill in the blanks ... */
|
|
|
|
header[8] = drawable->width % 256;
|
|
|
|
header[9] = drawable->width / 256;
|
|
|
|
header[10] = drawable->height % 256;
|
|
|
|
header[11] = drawable->height / 256;
|
|
|
|
header[12] = offx % 256;
|
|
|
|
header[13] = offx / 256;
|
|
|
|
header[14] = offy % 256;
|
|
|
|
header[15] = offy / 256;
|
|
|
|
fwrite (header, 32, 1, fp);
|
|
|
|
|
|
|
|
/* Arrange for memory etc. */
|
|
|
|
gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0, drawable->width,
|
2003-12-24 06:07:06 +08:00
|
|
|
drawable->height, TRUE, FALSE);
|
2004-01-06 01:37:42 +08:00
|
|
|
buffer = g_new (guchar, drawable->width*4);
|
|
|
|
line = g_new (guchar, (drawable->width+1) * 4);
|
2000-01-18 01:02:26 +08:00
|
|
|
|
2007-06-06 16:44:52 +08:00
|
|
|
/* Get the image from GIMP one line at a time and write it out */
|
2000-01-18 01:02:26 +08:00
|
|
|
for (i = 0; i < drawable->height; ++i)
|
|
|
|
{
|
|
|
|
gimp_pixel_rgn_get_rect (&pixel_rgn, line, 0, i, drawable->width, 1);
|
|
|
|
memset (buffer, 0, drawable->width);
|
|
|
|
|
2004-01-06 01:37:42 +08:00
|
|
|
if (bpp == 32)
|
|
|
|
{
|
|
|
|
for (j = 0; j < drawable->width; j++)
|
|
|
|
{
|
|
|
|
buffer[4*j] = line[4*j+2]; /* B */
|
2004-01-06 02:54:30 +08:00
|
|
|
buffer[4*j+1] = line[4*j+1]; /* G */
|
2004-01-06 01:37:42 +08:00
|
|
|
buffer[4*j+2] = line[4*j+0]; /* R */
|
|
|
|
buffer[4*j+3] = line[4*j+3]; /* Alpha */
|
|
|
|
}
|
|
|
|
fwrite (buffer, drawable->width, 4, fp);
|
|
|
|
}
|
|
|
|
else if (colours > 16)
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
|
|
|
for (j = 0, k = 0; j < drawable->width*2; j+= 2, ++k)
|
|
|
|
{
|
|
|
|
if (line[j+1] > 127)
|
|
|
|
{
|
|
|
|
buffer[k]= line[j] + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fwrite (buffer, drawable->width, 1, fp);
|
|
|
|
}
|
2000-01-18 01:02:26 +08:00
|
|
|
else
|
2003-12-24 06:07:06 +08:00
|
|
|
{
|
|
|
|
for (j = 0, k = 0; j < drawable->width*2; j+= 4, ++k)
|
|
|
|
{
|
|
|
|
buffer[k] = 0;
|
|
|
|
if (line[j+1] > 127)
|
|
|
|
{
|
|
|
|
buffer[k] += (line[j] + 1)<< 4;
|
|
|
|
}
|
|
|
|
if (line[j+3] > 127)
|
|
|
|
{
|
|
|
|
buffer[k] += (line[j+2] + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fwrite (buffer, (drawable->width+1)/2, 1, fp);
|
|
|
|
}
|
2000-01-18 01:02:26 +08:00
|
|
|
|
|
|
|
gimp_progress_update ((float) i / (float) drawable->height);
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-01-18 01:02:26 +08:00
|
|
|
/* Close files, give back allocated memory */
|
|
|
|
fclose (fp);
|
|
|
|
g_free (buffer);
|
|
|
|
g_free (line);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
1998-04-26 17:35:56 +08:00
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
static void
|
2003-07-02 20:03:08 +08:00
|
|
|
palette_dialog (const gchar *title)
|
2000-01-07 00:40:17 +08:00
|
|
|
{
|
1998-04-26 17:35:56 +08:00
|
|
|
GtkWidget *dialog;
|
|
|
|
|
2005-08-14 02:29:14 +08:00
|
|
|
gimp_ui_init (PLUG_IN_BINARY, FALSE);
|
1998-04-26 17:35:56 +08:00
|
|
|
|
2004-05-18 02:49:44 +08:00
|
|
|
dialog = gtk_file_chooser_dialog_new (title, NULL,
|
|
|
|
GTK_FILE_CHOOSER_ACTION_OPEN,
|
1998-04-26 17:35:56 +08:00
|
|
|
|
2004-05-18 02:49:44 +08:00
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
|
|
|
|
GTK_STOCK_OPEN, GTK_RESPONSE_OK,
|
|
|
|
|
|
|
|
NULL);
|
|
|
|
|
2005-02-09 04:40:33 +08:00
|
|
|
gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
|
2005-08-14 02:29:14 +08:00
|
|
|
GTK_RESPONSE_OK,
|
|
|
|
GTK_RESPONSE_CANCEL,
|
|
|
|
-1);
|
2005-02-09 04:40:33 +08:00
|
|
|
|
2004-05-18 02:49:44 +08:00
|
|
|
gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (dialog), palette_file);
|
|
|
|
|
|
|
|
gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
|
2000-01-07 00:40:17 +08:00
|
|
|
|
|
|
|
gtk_widget_show (dialog);
|
1998-04-26 17:35:56 +08:00
|
|
|
|
2003-11-19 22:51:52 +08:00
|
|
|
if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK)
|
|
|
|
{
|
|
|
|
g_free (palette_file);
|
2004-05-18 02:49:44 +08:00
|
|
|
palette_file = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
|
2003-11-19 22:51:52 +08:00
|
|
|
data_length = strlen (palette_file) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_widget_destroy (dialog);
|
1998-04-26 17:35:56 +08:00
|
|
|
}
|