2005-06-01 03:10:46 +08:00
|
|
|
/*
|
|
|
|
* pcx.c GIMP plug-in for loading & saving PCX files
|
|
|
|
*
|
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.
|
|
|
|
*
|
|
|
|
* 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/>.
|
2005-06-01 03:10:46 +08:00
|
|
|
*/
|
1998-01-25 09:24:46 +08:00
|
|
|
|
|
|
|
/* This code is based in parts on code by Francisco Bustamante, but the
|
|
|
|
largest portion of the code has been rewritten and is now maintained
|
1998-05-31 14:49:20 +08:00
|
|
|
occasionally by Nick Lamb njl195@zepler.org.uk */
|
1998-01-25 09:24:46 +08:00
|
|
|
|
2000-01-26 01:46:56 +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>
|
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-01 15:35:13 +08:00
|
|
|
#include "libgimp/stdplugins-intl.h"
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-05-02 01:01:18 +08:00
|
|
|
|
2005-08-15 19:07:27 +08:00
|
|
|
#define LOAD_PROC "file-pcx-load"
|
|
|
|
#define SAVE_PROC "file-pcx-save"
|
2008-08-11 18:06:13 +08:00
|
|
|
#define PLUG_IN_BINARY "file-pcx"
|
2011-04-09 02:31:34 +08:00
|
|
|
#define PLUG_IN_ROLE "gimp-file-pcx"
|
1998-01-25 09:24:46 +08:00
|
|
|
|
2012-11-20 05:26:04 +08:00
|
|
|
|
2007-08-29 20:01:01 +08:00
|
|
|
/* Declare local functions. */
|
2005-08-15 19:07:27 +08:00
|
|
|
|
|
|
|
static void query (void);
|
|
|
|
static void run (const gchar *name,
|
|
|
|
gint nparams,
|
|
|
|
const GimpParam *param,
|
|
|
|
gint *nreturn_vals,
|
|
|
|
GimpParam **return_vals);
|
|
|
|
|
2008-08-18 14:31:03 +08:00
|
|
|
static gint32 load_image (const gchar *filename,
|
|
|
|
GError **error);
|
|
|
|
|
2005-08-15 19:07:27 +08:00
|
|
|
static void load_1 (FILE *fp,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
2012-11-20 05:26:04 +08:00
|
|
|
guchar *buf,
|
2007-08-30 19:49:52 +08:00
|
|
|
guint16 bytes);
|
2005-08-15 19:07:27 +08:00
|
|
|
static void load_4 (FILE *fp,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
2012-11-20 05:26:04 +08:00
|
|
|
guchar *buf,
|
2007-08-30 19:49:52 +08:00
|
|
|
guint16 bytes);
|
2005-08-15 19:07:27 +08:00
|
|
|
static void load_8 (FILE *fp,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
2012-11-20 05:26:04 +08:00
|
|
|
guchar *buf,
|
2007-08-30 19:49:52 +08:00
|
|
|
guint16 bytes);
|
2005-08-15 19:07:27 +08:00
|
|
|
static void load_24 (FILE *fp,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
2012-11-20 05:26:04 +08:00
|
|
|
guchar *buf,
|
2007-08-30 19:49:52 +08:00
|
|
|
guint16 bytes);
|
2005-08-15 19:07:27 +08:00
|
|
|
static void readline (FILE *fp,
|
2012-11-20 05:26:04 +08:00
|
|
|
guchar *buf,
|
2005-08-15 19:07:27 +08:00
|
|
|
gint bytes);
|
|
|
|
|
|
|
|
static gint save_image (const gchar *filename,
|
|
|
|
gint32 image,
|
2008-08-18 14:31:03 +08:00
|
|
|
gint32 layer,
|
|
|
|
GError **error);
|
|
|
|
|
2005-08-15 19:07:27 +08:00
|
|
|
static void save_8 (FILE *fp,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
2012-11-20 05:26:04 +08:00
|
|
|
const guchar *buf);
|
2005-08-15 19:07:27 +08:00
|
|
|
static void save_24 (FILE *fp,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
2012-11-20 05:26:04 +08:00
|
|
|
const guchar *buf);
|
2005-08-15 19:07:27 +08:00
|
|
|
static void writeline (FILE *fp,
|
2012-11-20 05:26:04 +08:00
|
|
|
const guchar *buf,
|
2005-08-15 19:07:27 +08:00
|
|
|
gint bytes);
|
|
|
|
|
|
|
|
|
2006-05-16 20:26:20 +08:00
|
|
|
const GimpPlugInInfo PLUG_IN_INFO =
|
2000-01-26 01:46:56 +08:00
|
|
|
{
|
|
|
|
NULL, /* init_proc */
|
|
|
|
NULL, /* quit_proc */
|
|
|
|
query, /* query_proc */
|
|
|
|
run, /* run_proc */
|
|
|
|
};
|
1997-11-25 06:05:25 +08:00
|
|
|
|
|
|
|
MAIN ()
|
|
|
|
|
2000-01-26 01:46:56 +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-15 19:07:27 +08:00
|
|
|
{ GIMP_PDB_STRING, "filename", "The name of the file to load" },
|
|
|
|
{ GIMP_PDB_STRING, "raw-filename", "The name entered" }
|
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
|
|
|
};
|
|
|
|
|
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-15 19:07:27 +08:00
|
|
|
{ GIMP_PDB_IMAGE, "image", "Input image" },
|
|
|
|
{ GIMP_PDB_DRAWABLE, "drawable", "Drawable to save" },
|
|
|
|
{ GIMP_PDB_STRING, "filename", "The name of the file to save the image in" },
|
|
|
|
{ GIMP_PDB_STRING, "raw-filename", "The name entered" }
|
1997-11-25 06:05:25 +08:00
|
|
|
};
|
|
|
|
|
2005-08-15 19:07:27 +08:00
|
|
|
gimp_install_procedure (LOAD_PROC,
|
2000-01-31 10:32:30 +08:00
|
|
|
"Loads files in Zsoft PCX file format",
|
1997-11-25 06:05:25 +08:00
|
|
|
"FIXME: write help for pcx_load",
|
1998-01-25 09:24:46 +08:00
|
|
|
"Francisco Bustamante & Nick Lamb",
|
1998-05-31 14:49:20 +08:00
|
|
|
"Nick Lamb <njl195@zepler.org.uk>",
|
1998-03-15 07:21:07 +08:00
|
|
|
"January 1997",
|
2004-05-15 00:54:33 +08:00
|
|
|
N_("ZSoft PCX image"),
|
2008-05-11 02:16:33 +08:00
|
|
|
NULL,
|
2000-08-22 09:26:57 +08:00
|
|
|
GIMP_PLUGIN,
|
2001-12-06 10:28:58 +08:00
|
|
|
G_N_ELEMENTS (load_args),
|
|
|
|
G_N_ELEMENTS (load_return_vals),
|
1997-11-25 06:05:25 +08:00
|
|
|
load_args, load_return_vals);
|
|
|
|
|
2005-08-15 19:07:27 +08:00
|
|
|
gimp_register_file_handler_mime (LOAD_PROC, "image/x-pcx");
|
|
|
|
gimp_register_magic_load_handler (LOAD_PROC,
|
2008-05-11 02:16:33 +08:00
|
|
|
"pcx,pcc",
|
|
|
|
"",
|
|
|
|
"0&,byte,10,2&,byte,1,3&,byte,>0,3,byte,<9");
|
2004-05-15 00:54:33 +08:00
|
|
|
|
2005-08-15 19:07:27 +08:00
|
|
|
gimp_install_procedure (SAVE_PROC,
|
2000-01-31 10:32:30 +08:00
|
|
|
"Saves files in ZSoft PCX file format",
|
1997-11-25 06:05:25 +08:00
|
|
|
"FIXME: write help for pcx_save",
|
1998-01-25 09:24:46 +08:00
|
|
|
"Francisco Bustamante & Nick Lamb",
|
1998-05-31 14:49:20 +08:00
|
|
|
"Nick Lamb <njl195@zepler.org.uk>",
|
1998-03-15 07:21:07 +08:00
|
|
|
"January 1997",
|
2004-05-15 00:54:33 +08:00
|
|
|
N_("ZSoft PCX image"),
|
2008-05-11 02:16:33 +08:00
|
|
|
"INDEXED, RGB, GRAY",
|
2000-08-22 09:26:57 +08:00
|
|
|
GIMP_PLUGIN,
|
2001-12-06 10:28:58 +08:00
|
|
|
G_N_ELEMENTS (save_args), 0,
|
1997-11-25 06:05:25 +08:00
|
|
|
save_args, NULL);
|
|
|
|
|
2005-08-15 19:07:27 +08:00
|
|
|
gimp_register_file_handler_mime (SAVE_PROC, "image/x-pcx");
|
|
|
|
gimp_register_save_handler (SAVE_PROC, "pcx,pcc", "");
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
static void
|
2003-11-06 23:27:05 +08:00
|
|
|
run (const gchar *name,
|
|
|
|
gint nparams,
|
|
|
|
const GimpParam *param,
|
2003-07-02 20:03:08 +08:00
|
|
|
gint *nreturn_vals,
|
2003-11-06 23:27:05 +08:00
|
|
|
GimpParam **return_vals)
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
2008-08-18 14:31:03 +08:00
|
|
|
static GimpParam values[2];
|
|
|
|
GimpRunMode run_mode;
|
|
|
|
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
|
|
|
|
gint32 image_ID;
|
|
|
|
gint32 drawable_ID;
|
|
|
|
GimpExportReturn export = GIMP_EXPORT_CANCEL;
|
|
|
|
GError *error = NULL;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-03-26 00:38:19 +08:00
|
|
|
INIT_I18N ();
|
2012-11-20 05:26:04 +08:00
|
|
|
gegl_init (NULL, NULL);
|
|
|
|
|
|
|
|
run_mode = param[0].data.d_int32;
|
2003-03-26 00:38:19 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
*nreturn_vals = 1;
|
2000-01-26 01:46:56 +08:00
|
|
|
*return_vals = values;
|
2005-08-15 19:07:27 +08:00
|
|
|
|
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-15 19:07:27 +08:00
|
|
|
if (strcmp (name, LOAD_PROC) == 0)
|
2000-01-26 01:46:56 +08:00
|
|
|
{
|
2008-08-18 14:31:03 +08:00
|
|
|
image_ID = load_image (param[1].data.d_string, &error);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
if (image_ID != -1)
|
2008-05-11 02:16:33 +08:00
|
|
|
{
|
|
|
|
*nreturn_vals = 2;
|
|
|
|
values[1].type = GIMP_PDB_IMAGE;
|
|
|
|
values[1].data.d_image = image_ID;
|
|
|
|
}
|
2000-01-26 01:46:56 +08:00
|
|
|
else
|
2008-05-11 02:16:33 +08:00
|
|
|
{
|
|
|
|
status = GIMP_PDB_EXECUTION_ERROR;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
2005-08-15 19:07:27 +08:00
|
|
|
else if (strcmp (name, SAVE_PROC) == 0)
|
1997-11-25 06:05:25 +08:00
|
|
|
{
|
1999-10-10 03:06:14 +08:00
|
|
|
image_ID = param[1].data.d_int32;
|
|
|
|
drawable_ID = param[2].data.d_int32;
|
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
/* eventually export the image */
|
1999-10-10 03:06:14 +08:00
|
|
|
switch (run_mode)
|
2008-05-11 02:16:33 +08:00
|
|
|
{
|
|
|
|
case GIMP_RUN_INTERACTIVE:
|
|
|
|
case GIMP_RUN_WITH_LAST_VALS:
|
|
|
|
gimp_ui_init (PLUG_IN_BINARY, FALSE);
|
2008-08-18 14:31:03 +08:00
|
|
|
|
2013-11-10 07:18:48 +08:00
|
|
|
export = gimp_export_image (&image_ID, &drawable_ID, "PCX",
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_RGB |
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_GRAY |
|
|
|
|
GIMP_EXPORT_CAN_HANDLE_INDEXED);
|
|
|
|
|
2008-05-11 02:16:33 +08:00
|
|
|
if (export == GIMP_EXPORT_CANCEL)
|
|
|
|
{
|
|
|
|
values[0].data.d_status = GIMP_PDB_CANCEL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
1999-10-10 03:06:14 +08:00
|
|
|
|
1997-11-25 06:05:25 +08:00
|
|
|
switch (run_mode)
|
2008-05-11 02:16:33 +08:00
|
|
|
{
|
|
|
|
case GIMP_RUN_INTERACTIVE:
|
|
|
|
break;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2008-05-11 02:16:33 +08:00
|
|
|
case GIMP_RUN_NONINTERACTIVE:
|
|
|
|
if (nparams != 5)
|
|
|
|
status = GIMP_PDB_CALLING_ERROR;
|
|
|
|
break;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2008-05-11 02:16:33 +08:00
|
|
|
case GIMP_RUN_WITH_LAST_VALS:
|
|
|
|
break;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2008-05-11 02:16:33 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-08-22 09:26:57 +08:00
|
|
|
if (status == GIMP_PDB_SUCCESS)
|
2008-05-11 02:16:33 +08:00
|
|
|
{
|
2008-08-18 14:31:03 +08:00
|
|
|
if (! save_image (param[3].data.d_string, image_ID, drawable_ID,
|
|
|
|
&error))
|
2008-05-11 02:16:33 +08:00
|
|
|
{
|
|
|
|
status = GIMP_PDB_EXECUTION_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2000-08-24 22:17:34 +08:00
|
|
|
if (export == GIMP_EXPORT_EXPORT)
|
2008-05-11 02:16:33 +08:00
|
|
|
gimp_image_delete (image_ID);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
2000-01-26 01:46:56 +08:00
|
|
|
else
|
|
|
|
{
|
2000-08-22 09:26:57 +08:00
|
|
|
status = GIMP_PDB_CALLING_ERROR;
|
2000-01-26 01:46:56 +08:00
|
|
|
}
|
|
|
|
|
2008-08-18 14:31:03 +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
|
|
|
}
|
|
|
|
|
2009-02-25 18:56:41 +08:00
|
|
|
static const guchar mono[6]= { 0, 0, 0, 255, 255, 255 };
|
2000-01-26 01:46:56 +08:00
|
|
|
|
|
|
|
static struct
|
|
|
|
{
|
2007-08-30 19:49:52 +08:00
|
|
|
guint8 manufacturer;
|
|
|
|
guint8 version;
|
|
|
|
guint8 compression;
|
|
|
|
guint8 bpp;
|
|
|
|
guint16 x1, y1;
|
|
|
|
guint16 x2, y2;
|
|
|
|
guint16 hdpi;
|
|
|
|
guint16 vdpi;
|
|
|
|
guint8 colormap[48];
|
|
|
|
guint8 reserved;
|
|
|
|
guint8 planes;
|
|
|
|
guint16 bytesperline;
|
|
|
|
guint16 color;
|
|
|
|
guint8 filler[58];
|
1997-11-25 06:05:25 +08:00
|
|
|
} pcx_header;
|
|
|
|
|
2007-08-30 19:49:52 +08:00
|
|
|
static struct {
|
|
|
|
size_t size;
|
|
|
|
gpointer address;
|
2009-02-25 18:56:41 +08:00
|
|
|
} const pcx_header_buf_xlate[] = {
|
2007-08-30 19:49:52 +08:00
|
|
|
{ 1, &pcx_header.manufacturer },
|
2009-02-25 18:56:41 +08:00
|
|
|
{ 1, &pcx_header.version },
|
|
|
|
{ 1, &pcx_header.compression },
|
|
|
|
{ 1, &pcx_header.bpp },
|
|
|
|
{ 2, &pcx_header.x1 },
|
|
|
|
{ 2, &pcx_header.y1 },
|
|
|
|
{ 2, &pcx_header.x2 },
|
|
|
|
{ 2, &pcx_header.y2 },
|
|
|
|
{ 2, &pcx_header.hdpi },
|
|
|
|
{ 2, &pcx_header.vdpi },
|
|
|
|
{ 48, &pcx_header.colormap },
|
|
|
|
{ 1, &pcx_header.reserved },
|
|
|
|
{ 1, &pcx_header.planes },
|
2007-08-30 19:49:52 +08:00
|
|
|
{ 2, &pcx_header.bytesperline },
|
2009-02-25 18:56:41 +08:00
|
|
|
{ 2, &pcx_header.color },
|
|
|
|
{ 58, &pcx_header.filler },
|
2007-08-30 19:49:52 +08:00
|
|
|
{ 0, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2007-09-24 01:02:11 +08:00
|
|
|
pcx_header_from_buffer (guint8 *buf)
|
2007-08-30 19:49:52 +08:00
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
gint buf_offset = 0;
|
|
|
|
|
|
|
|
for (i = 0; pcx_header_buf_xlate[i].size != 0; i++)
|
|
|
|
{
|
|
|
|
g_memmove (pcx_header_buf_xlate[i].address, buf + buf_offset,
|
|
|
|
pcx_header_buf_xlate[i].size);
|
|
|
|
buf_offset += pcx_header_buf_xlate[i].size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-09-24 01:02:11 +08:00
|
|
|
pcx_header_to_buffer (guint8 *buf)
|
2007-08-30 19:49:52 +08:00
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
gint buf_offset = 0;
|
|
|
|
|
|
|
|
for (i = 0; pcx_header_buf_xlate[i].size != 0; i++)
|
|
|
|
{
|
|
|
|
g_memmove (buf + buf_offset, pcx_header_buf_xlate[i].address,
|
|
|
|
pcx_header_buf_xlate[i].size);
|
|
|
|
buf_offset += pcx_header_buf_xlate[i].size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
static gint32
|
2008-08-18 14:31:03 +08:00
|
|
|
load_image (const gchar *filename,
|
|
|
|
GError **error)
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
2005-08-15 19:07:27 +08:00
|
|
|
FILE *fd;
|
2012-11-20 05:26:04 +08:00
|
|
|
GeglBuffer *buffer;
|
2007-09-18 02:56:34 +08:00
|
|
|
guint16 offset_x, offset_y, bytesperline;
|
2007-08-30 19:49:52 +08:00
|
|
|
gint32 width, height;
|
2013-11-01 20:51:31 +08:00
|
|
|
guint16 resolution_x, resolution_y;
|
2005-08-15 19:07:27 +08:00
|
|
|
gint32 image, layer;
|
|
|
|
guchar *dest, cmap[768];
|
2007-08-30 19:49:52 +08:00
|
|
|
guint8 header_buf[128];
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2014-07-23 22:39:00 +08:00
|
|
|
gimp_progress_init_printf (_("Opening '%s'"),
|
|
|
|
gimp_filename_to_utf8 (filename));
|
|
|
|
|
2005-03-04 23:12:29 +08:00
|
|
|
fd = g_fopen (filename, "rb");
|
2008-08-18 14:31:03 +08:00
|
|
|
|
|
|
|
if (! fd)
|
2000-01-26 01:46:56 +08:00
|
|
|
{
|
2008-08-18 14:31:03 +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 (filename), g_strerror (errno));
|
2000-01-26 01:46:56 +08:00
|
|
|
return -1;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2007-08-30 19:49:52 +08:00
|
|
|
if (fread (header_buf, 128, 1, fd) == 0)
|
2000-01-26 01:46:56 +08:00
|
|
|
{
|
2008-08-18 14:31:03 +08:00
|
|
|
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
|
|
|
|
_("Could not read header from '%s'"),
|
|
|
|
gimp_filename_to_utf8 (filename));
|
2010-11-10 02:03:47 +08:00
|
|
|
fclose (fd);
|
2000-01-26 01:46:56 +08:00
|
|
|
return -1;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2007-08-30 19:49:52 +08:00
|
|
|
pcx_header_from_buffer (header_buf);
|
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
if (pcx_header.manufacturer != 10)
|
|
|
|
{
|
2008-08-18 14:31:03 +08:00
|
|
|
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
|
|
|
|
_("'%s' is not a PCX file"),
|
|
|
|
gimp_filename_to_utf8 (filename));
|
2010-11-10 02:03:47 +08:00
|
|
|
fclose (fd);
|
2000-01-26 01:46:56 +08:00
|
|
|
return -1;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2009-02-25 18:56:41 +08:00
|
|
|
offset_x = GUINT16_FROM_LE (pcx_header.x1);
|
|
|
|
offset_y = GUINT16_FROM_LE (pcx_header.y1);
|
|
|
|
width = GUINT16_FROM_LE (pcx_header.x2) - offset_x + 1;
|
|
|
|
height = GUINT16_FROM_LE (pcx_header.y2) - offset_y + 1;
|
2007-09-18 02:56:34 +08:00
|
|
|
bytesperline = GUINT16_FROM_LE (pcx_header.bytesperline);
|
2013-11-01 20:51:31 +08:00
|
|
|
resolution_x = GUINT16_FROM_LE (pcx_header.hdpi);
|
|
|
|
resolution_y = GUINT16_FROM_LE (pcx_header.vdpi);
|
1998-01-25 09:24:46 +08:00
|
|
|
|
2007-08-30 19:49:52 +08:00
|
|
|
if ((width < 0) || (width > GIMP_MAX_IMAGE_SIZE))
|
2007-07-06 01:01:41 +08:00
|
|
|
{
|
|
|
|
g_message (_("Unsupported or invalid image width: %d"), width);
|
2010-11-10 02:03:47 +08:00
|
|
|
fclose (fd);
|
2007-07-06 01:01:41 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2007-08-30 19:49:52 +08:00
|
|
|
if ((height < 0) || (height > GIMP_MAX_IMAGE_SIZE))
|
2007-07-06 01:01:41 +08:00
|
|
|
{
|
|
|
|
g_message (_("Unsupported or invalid image height: %d"), height);
|
2010-11-10 02:03:47 +08:00
|
|
|
fclose (fd);
|
2007-07-06 01:01:41 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2008-01-22 21:15:55 +08:00
|
|
|
if (bytesperline < (width * pcx_header.bpp) / 8)
|
2007-09-18 02:56:34 +08:00
|
|
|
{
|
|
|
|
g_message (_("Invalid number of bytes per line in PCX header"));
|
2010-11-10 02:03:47 +08:00
|
|
|
fclose (fd);
|
2007-09-18 02:56:34 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2013-11-01 20:51:31 +08:00
|
|
|
if ((resolution_x < 1) || (resolution_x > GIMP_MAX_RESOLUTION) ||
|
|
|
|
(resolution_y < 1) || (resolution_y > GIMP_MAX_RESOLUTION))
|
|
|
|
{
|
|
|
|
g_message (_("Resolution out of bounds in XCX header, using 72x72"));
|
|
|
|
resolution_x = 72;
|
|
|
|
resolution_y = 72;
|
|
|
|
}
|
2007-07-06 01:01:41 +08:00
|
|
|
|
2009-12-01 22:31:52 +08:00
|
|
|
/* Shield against potential buffer overflows in load_*() functions. */
|
|
|
|
if (G_MAXSIZE / width / height < 3)
|
|
|
|
{
|
|
|
|
g_message (_("Image dimensions too large: width %d x height %d"), width, height);
|
2010-11-10 02:03:47 +08:00
|
|
|
fclose (fd);
|
2009-12-01 22:31:52 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
if (pcx_header.planes == 3 && pcx_header.bpp == 8)
|
|
|
|
{
|
2000-08-22 09:26:57 +08:00
|
|
|
image= gimp_image_new (width, height, GIMP_RGB);
|
2000-01-26 01:46:56 +08:00
|
|
|
layer= gimp_layer_new (image, _("Background"), width, height,
|
2008-05-11 02:16:33 +08:00
|
|
|
GIMP_RGB_IMAGE, 100, GIMP_NORMAL_MODE);
|
2000-01-26 01:46:56 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-08-22 09:26:57 +08:00
|
|
|
image= gimp_image_new (width, height, GIMP_INDEXED);
|
2000-01-26 01:46:56 +08:00
|
|
|
layer= gimp_layer_new (image, _("Background"), width, height,
|
2008-05-11 02:16:33 +08:00
|
|
|
GIMP_INDEXED_IMAGE, 100, GIMP_NORMAL_MODE);
|
2000-01-26 01:46:56 +08:00
|
|
|
}
|
2013-11-01 20:51:31 +08:00
|
|
|
|
1998-01-25 09:24:46 +08:00
|
|
|
gimp_image_set_filename (image, filename);
|
2013-11-01 20:51:31 +08:00
|
|
|
gimp_image_set_resolution (image, resolution_x, resolution_y);
|
|
|
|
|
2010-09-06 17:40:46 +08:00
|
|
|
gimp_image_insert_layer (image, layer, -1, 0);
|
1998-01-25 09:24:46 +08:00
|
|
|
gimp_layer_set_offsets (layer, offset_x, offset_y);
|
2012-11-20 05:26:04 +08:00
|
|
|
|
|
|
|
buffer = gimp_drawable_get_buffer (layer);
|
1998-01-25 09:24:46 +08:00
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
if (pcx_header.planes == 1 && pcx_header.bpp == 1)
|
|
|
|
{
|
2009-12-02 22:12:17 +08:00
|
|
|
dest = g_new (guchar, ((gsize) width) * height);
|
2007-09-18 02:56:34 +08:00
|
|
|
load_1 (fd, width, height, dest, bytesperline);
|
2004-11-02 20:00:25 +08:00
|
|
|
gimp_image_set_colormap (image, mono, 2);
|
2000-01-26 01:46:56 +08:00
|
|
|
}
|
|
|
|
else if (pcx_header.planes == 4 && pcx_header.bpp == 1)
|
|
|
|
{
|
2009-12-02 22:12:17 +08:00
|
|
|
dest = g_new (guchar, ((gsize) width) * height);
|
2007-09-18 02:56:34 +08:00
|
|
|
load_4 (fd, width, height, dest, bytesperline);
|
2004-11-02 20:00:25 +08:00
|
|
|
gimp_image_set_colormap (image, pcx_header.colormap, 16);
|
2000-01-26 01:46:56 +08:00
|
|
|
}
|
|
|
|
else if (pcx_header.planes == 1 && pcx_header.bpp == 8)
|
|
|
|
{
|
2009-12-02 22:12:17 +08:00
|
|
|
dest = g_new (guchar, ((gsize) width) * height);
|
2007-09-18 02:56:34 +08:00
|
|
|
load_8 (fd, width, height, dest, bytesperline);
|
2007-08-30 19:49:52 +08:00
|
|
|
fseek (fd, -768L, SEEK_END);
|
|
|
|
fread (cmap, 768, 1, fd);
|
2004-11-02 20:00:25 +08:00
|
|
|
gimp_image_set_colormap (image, cmap, 256);
|
2000-01-26 01:46:56 +08:00
|
|
|
}
|
|
|
|
else if (pcx_header.planes == 3 && pcx_header.bpp == 8)
|
|
|
|
{
|
2009-12-02 22:12:17 +08:00
|
|
|
dest = g_new (guchar, ((gsize) width) * height * 3);
|
2007-09-18 02:56:34 +08:00
|
|
|
load_24 (fd, width, height, dest, bytesperline);
|
2000-01-26 01:46:56 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-06-13 22:37:00 +08:00
|
|
|
g_message (_("Unusual PCX flavour, giving up"));
|
2010-11-10 02:03:47 +08:00
|
|
|
fclose (fd);
|
2000-01-26 01:46:56 +08:00
|
|
|
return -1;
|
|
|
|
}
|
1998-01-25 09:24:46 +08:00
|
|
|
|
2012-11-20 05:26:04 +08:00
|
|
|
gegl_buffer_set (buffer, GEGL_RECTANGLE (0, 0, width, height), 0,
|
|
|
|
NULL, dest, GEGL_AUTO_ROWSTRIDE);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2010-11-10 02:03:47 +08:00
|
|
|
fclose (fd);
|
1998-01-25 09:24:46 +08:00
|
|
|
g_free (dest);
|
2012-11-20 05:26:04 +08:00
|
|
|
g_object_unref (buffer);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2012-11-20 05:26:04 +08:00
|
|
|
gimp_progress_update (1.0);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
1998-01-25 09:24:46 +08:00
|
|
|
return image;
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
static void
|
2007-08-30 19:49:52 +08:00
|
|
|
load_8 (FILE *fp,
|
2008-05-11 02:16:33 +08:00
|
|
|
gint width,
|
|
|
|
gint height,
|
2012-11-20 05:26:04 +08:00
|
|
|
guchar *buf,
|
2008-05-11 02:16:33 +08:00
|
|
|
guint16 bytes)
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
2003-07-03 21:26:06 +08:00
|
|
|
gint row;
|
2007-08-30 19:49:52 +08:00
|
|
|
guchar *line = g_new (guchar, bytes);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2012-11-20 05:26:04 +08:00
|
|
|
for (row = 0; row < height; buf += width, ++row)
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
2000-01-26 01:46:56 +08:00
|
|
|
readline (fp, line, bytes);
|
2012-11-20 05:26:04 +08:00
|
|
|
memcpy (buf, line, width);
|
1999-10-10 03:06:14 +08:00
|
|
|
gimp_progress_update ((double) row / (double) height);
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
g_free (line);
|
1998-01-25 09:24:46 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
static void
|
2007-08-30 19:49:52 +08:00
|
|
|
load_24 (FILE *fp,
|
2008-05-11 02:16:33 +08:00
|
|
|
gint width,
|
|
|
|
gint height,
|
2012-11-20 05:26:04 +08:00
|
|
|
guchar *buf,
|
2008-05-11 02:16:33 +08:00
|
|
|
guint16 bytes)
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
2003-07-03 21:26:06 +08:00
|
|
|
gint x, y, c;
|
2007-08-30 19:49:52 +08:00
|
|
|
guchar *line = g_new (guchar, bytes);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2012-11-20 05:26:04 +08:00
|
|
|
for (y = 0; y < height; buf += width * 3, ++y)
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
2003-11-06 23:27:05 +08:00
|
|
|
for (c = 0; c < 3; ++c)
|
2008-05-11 02:16:33 +08:00
|
|
|
{
|
|
|
|
readline (fp, line, bytes);
|
|
|
|
for (x = 0; x < width; ++x)
|
|
|
|
{
|
2012-11-20 05:26:04 +08:00
|
|
|
buf[x * 3 + c] = line[x];
|
2008-05-11 02:16:33 +08:00
|
|
|
}
|
|
|
|
}
|
1999-10-10 03:06:14 +08:00
|
|
|
gimp_progress_update ((double) y / (double) height);
|
1998-01-25 09:24:46 +08:00
|
|
|
}
|
2000-01-26 01:46:56 +08:00
|
|
|
|
|
|
|
g_free (line);
|
1998-01-25 09:24:46 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
static void
|
2007-08-30 19:49:52 +08:00
|
|
|
load_1 (FILE *fp,
|
2008-05-11 02:16:33 +08:00
|
|
|
gint width,
|
|
|
|
gint height,
|
2012-11-20 05:26:04 +08:00
|
|
|
guchar *buf,
|
2008-05-11 02:16:33 +08:00
|
|
|
guint16 bytes)
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
2003-07-03 21:26:06 +08:00
|
|
|
gint x, y;
|
|
|
|
guchar *line = g_new (guchar, bytes);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2012-11-20 05:26:04 +08:00
|
|
|
for (y = 0; y < height; buf += width, ++y)
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
2000-01-26 01:46:56 +08:00
|
|
|
readline (fp, line, bytes);
|
2003-11-06 23:27:05 +08:00
|
|
|
for (x = 0; x < width; ++x)
|
2008-05-11 02:16:33 +08:00
|
|
|
{
|
|
|
|
if (line[x / 8] & (128 >> (x % 8)))
|
2012-11-20 05:26:04 +08:00
|
|
|
buf[x] = 1;
|
2008-05-11 02:16:33 +08:00
|
|
|
else
|
2012-11-20 05:26:04 +08:00
|
|
|
buf[x] = 0;
|
2008-05-11 02:16:33 +08:00
|
|
|
}
|
1999-10-10 03:06:14 +08:00
|
|
|
gimp_progress_update ((double) y / (double) height);
|
1998-01-25 09:24:46 +08:00
|
|
|
}
|
2000-01-26 01:46:56 +08:00
|
|
|
|
|
|
|
g_free (line);
|
1998-01-25 09:24:46 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
static void
|
2007-08-30 19:49:52 +08:00
|
|
|
load_4 (FILE *fp,
|
2008-05-11 02:16:33 +08:00
|
|
|
gint width,
|
|
|
|
gint height,
|
2012-11-20 05:26:04 +08:00
|
|
|
guchar *buf,
|
2008-05-11 02:16:33 +08:00
|
|
|
guint16 bytes)
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
2003-07-03 21:26:06 +08:00
|
|
|
gint x, y, c;
|
2007-08-30 19:49:52 +08:00
|
|
|
guchar *line = g_new (guchar, bytes);
|
1998-01-25 09:24:46 +08:00
|
|
|
|
2012-11-20 05:26:04 +08:00
|
|
|
for (y = 0; y < height; buf += width, ++y)
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
2007-08-30 19:49:52 +08:00
|
|
|
for (x = 0; x < width; ++x)
|
2012-11-20 05:26:04 +08:00
|
|
|
buf[x] = 0;
|
2003-11-06 23:27:05 +08:00
|
|
|
for (c = 0; c < 4; ++c)
|
2008-05-11 02:16:33 +08:00
|
|
|
{
|
|
|
|
readline(fp, line, bytes);
|
|
|
|
for (x = 0; x < width; ++x)
|
|
|
|
{
|
|
|
|
if (line[x / 8] & (128 >> (x % 8)))
|
2012-11-20 05:26:04 +08:00
|
|
|
buf[x] += (1 << c);
|
2008-05-11 02:16:33 +08:00
|
|
|
}
|
|
|
|
}
|
1999-10-10 03:06:14 +08:00
|
|
|
gimp_progress_update ((double) y / (double) height);
|
1998-01-25 09:24:46 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
g_free (line);
|
1998-01-25 09:24:46 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
static void
|
2003-11-06 23:27:05 +08:00
|
|
|
readline (FILE *fp,
|
2012-11-20 05:26:04 +08:00
|
|
|
guchar *buf,
|
2008-05-11 02:16:33 +08:00
|
|
|
gint bytes)
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
2000-01-26 01:46:56 +08:00
|
|
|
static guchar count = 0, value = 0;
|
1998-01-25 09:24:46 +08:00
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
if (pcx_header.compression)
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
2003-11-06 23:27:05 +08:00
|
|
|
while (bytes--)
|
2008-05-11 02:16:33 +08:00
|
|
|
{
|
|
|
|
if (count == 0)
|
|
|
|
{
|
|
|
|
value = fgetc (fp);
|
|
|
|
if (value < 0xc0)
|
|
|
|
{
|
|
|
|
count = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
count = value - 0xc0;
|
|
|
|
value = fgetc (fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count--;
|
2012-11-20 05:26:04 +08:00
|
|
|
*(buf++) = value;
|
2008-05-11 02:16:33 +08:00
|
|
|
}
|
2003-11-06 23:27:05 +08:00
|
|
|
}
|
|
|
|
else
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
2012-11-20 05:26:04 +08:00
|
|
|
fread (buf, bytes, 1, fp);
|
1998-01-25 09:24:46 +08:00
|
|
|
}
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
static gint
|
2008-08-18 14:31:03 +08:00
|
|
|
save_image (const gchar *filename,
|
|
|
|
gint32 image,
|
|
|
|
gint32 layer,
|
|
|
|
GError **error)
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
2005-08-15 19:07:27 +08:00
|
|
|
FILE *fp;
|
2012-11-20 05:26:04 +08:00
|
|
|
GeglBuffer *buffer;
|
|
|
|
const Babl *format;
|
2005-08-15 19:07:27 +08:00
|
|
|
GimpImageType drawable_type;
|
|
|
|
guchar *cmap= NULL;
|
|
|
|
guchar *pixels;
|
2007-08-30 19:49:52 +08:00
|
|
|
gint offset_x, offset_y;
|
|
|
|
guint width, height;
|
2013-11-01 20:51:31 +08:00
|
|
|
gdouble resolution_x, resolution_y;
|
2005-08-15 19:07:27 +08:00
|
|
|
gint colors, i;
|
2007-08-30 19:49:52 +08:00
|
|
|
guint8 header_buf[128];
|
1998-01-25 09:24:46 +08:00
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
drawable_type = gimp_drawable_type (layer);
|
1998-01-25 09:24:46 +08:00
|
|
|
gimp_drawable_offsets (layer, &offset_x, &offset_y);
|
2012-11-20 05:26:04 +08:00
|
|
|
|
|
|
|
buffer = gimp_drawable_get_buffer (layer);
|
|
|
|
|
|
|
|
width = gegl_buffer_get_width (buffer);
|
|
|
|
height = gegl_buffer_get_height (buffer);
|
1998-01-25 09:24:46 +08:00
|
|
|
|
2005-09-30 16:16:10 +08:00
|
|
|
gimp_progress_init_printf (_("Saving '%s'"),
|
2005-09-30 02:34:08 +08:00
|
|
|
gimp_filename_to_utf8 (filename));
|
1998-01-25 09:24:46 +08:00
|
|
|
|
|
|
|
pcx_header.manufacturer = 0x0a;
|
|
|
|
pcx_header.version = 5;
|
|
|
|
pcx_header.compression = 1;
|
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
switch (drawable_type)
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
2000-08-22 09:26:57 +08:00
|
|
|
case GIMP_INDEXED_IMAGE:
|
2012-11-20 05:26:04 +08:00
|
|
|
cmap = gimp_image_get_colormap (image, &colors);
|
|
|
|
pcx_header.bpp = 8;
|
|
|
|
pcx_header.planes = 1;
|
|
|
|
pcx_header.color = GUINT16_TO_LE (1);
|
2007-08-30 19:49:52 +08:00
|
|
|
pcx_header.bytesperline = GUINT16_TO_LE (width);
|
2012-11-20 05:26:04 +08:00
|
|
|
format = NULL;
|
1998-01-25 09:24:46 +08:00
|
|
|
break;
|
|
|
|
|
2000-08-22 09:26:57 +08:00
|
|
|
case GIMP_RGB_IMAGE:
|
2012-11-20 05:26:04 +08:00
|
|
|
pcx_header.bpp = 8;
|
|
|
|
pcx_header.planes = 3;
|
|
|
|
pcx_header.color = GUINT16_TO_LE (1);
|
2007-08-30 19:49:52 +08:00
|
|
|
pcx_header.bytesperline = GUINT16_TO_LE (width);
|
2012-11-20 05:26:04 +08:00
|
|
|
format = babl_format ("R'G'B' u8");
|
1998-01-25 09:24:46 +08:00
|
|
|
break;
|
|
|
|
|
2000-08-22 09:26:57 +08:00
|
|
|
case GIMP_GRAY_IMAGE:
|
2012-11-20 05:26:04 +08:00
|
|
|
pcx_header.bpp = 8;
|
|
|
|
pcx_header.planes = 1;
|
|
|
|
pcx_header.color = GUINT16_TO_LE (2);
|
2007-08-30 19:49:52 +08:00
|
|
|
pcx_header.bytesperline = GUINT16_TO_LE (width);
|
2012-11-20 05:26:04 +08:00
|
|
|
format = babl_format ("Y' u8");
|
1998-01-25 09:24:46 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2003-11-15 21:53:33 +08:00
|
|
|
g_message (_("Cannot save images with alpha channel."));
|
2000-01-26 01:46:56 +08:00
|
|
|
return FALSE;
|
1998-01-25 09:24:46 +08:00
|
|
|
}
|
2000-01-26 01:46:56 +08:00
|
|
|
|
|
|
|
pixels = (guchar *) g_malloc (width * height * pcx_header.planes);
|
2012-11-20 05:26:04 +08:00
|
|
|
|
|
|
|
gegl_buffer_get (buffer, GEGL_RECTANGLE (0, 0, width, height), 1.0,
|
|
|
|
format, pixels,
|
|
|
|
GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2007-08-30 19:49:52 +08:00
|
|
|
if ((offset_x < 0) || (offset_x > (1<<16)))
|
|
|
|
{
|
|
|
|
g_message (_("Invalid X offset: %d"), offset_x);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((offset_y < 0) || (offset_y > (1<<16)))
|
|
|
|
{
|
|
|
|
g_message (_("Invalid Y offset: %d"), offset_y);
|
|
|
|
return FALSE;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2007-08-30 19:49:52 +08:00
|
|
|
if (offset_x + width - 1 > (1<<16))
|
|
|
|
{
|
|
|
|
g_message (_("Right border out of bounds (must be < %d): %d"), (1<<16),
|
|
|
|
offset_x + width - 1);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset_y + height - 1 > (1<<16))
|
|
|
|
{
|
|
|
|
g_message (_("Bottom border out of bounds (must be < %d): %d"), (1<<16),
|
|
|
|
offset_y + height - 1);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-11-10 02:06:00 +08:00
|
|
|
if ((fp = g_fopen (filename, "wb")) == NULL)
|
|
|
|
{
|
|
|
|
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
|
|
|
|
_("Could not open '%s' for writing: %s"),
|
|
|
|
gimp_filename_to_utf8 (filename), g_strerror (errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2007-08-30 19:49:52 +08:00
|
|
|
pcx_header.x1 = GUINT16_TO_LE ((guint16)offset_x);
|
|
|
|
pcx_header.y1 = GUINT16_TO_LE ((guint16)offset_y);
|
|
|
|
pcx_header.x2 = GUINT16_TO_LE ((guint16)(offset_x + width - 1));
|
|
|
|
pcx_header.y2 = GUINT16_TO_LE ((guint16)(offset_y + height - 1));
|
|
|
|
|
2013-11-01 20:51:31 +08:00
|
|
|
gimp_image_get_resolution (image, &resolution_x, &resolution_y);
|
|
|
|
|
|
|
|
pcx_header.hdpi = GUINT16_TO_LE (RINT (MAX (resolution_x, 1.0)));
|
|
|
|
pcx_header.vdpi = GUINT16_TO_LE (RINT (MAX (resolution_y, 1.0)));
|
1998-01-25 09:24:46 +08:00
|
|
|
pcx_header.reserved = 0;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2007-08-30 19:49:52 +08:00
|
|
|
pcx_header_to_buffer (header_buf);
|
|
|
|
|
|
|
|
fwrite (header_buf, 128, 1, fp);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
switch (drawable_type)
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
2000-08-22 09:26:57 +08:00
|
|
|
case GIMP_INDEXED_IMAGE:
|
2000-01-26 01:46:56 +08:00
|
|
|
save_8 (fp, width, height, pixels);
|
|
|
|
fputc (0x0c, fp);
|
|
|
|
fwrite (cmap, colors, 3, fp);
|
2003-11-06 23:27:05 +08:00
|
|
|
for (i = colors; i < 256; i++)
|
2008-05-11 02:16:33 +08:00
|
|
|
{
|
2007-08-30 19:49:52 +08:00
|
|
|
fputc (0, fp);
|
|
|
|
fputc (0, fp);
|
2008-05-11 02:16:33 +08:00
|
|
|
fputc (0, fp);
|
|
|
|
}
|
1998-01-25 09:24:46 +08:00
|
|
|
break;
|
2003-06-13 22:37:00 +08:00
|
|
|
|
2000-08-22 09:26:57 +08:00
|
|
|
case GIMP_RGB_IMAGE:
|
2000-01-26 01:46:56 +08:00
|
|
|
save_24 (fp, width, height, pixels);
|
1998-01-25 09:24:46 +08:00
|
|
|
break;
|
2003-06-13 22:37:00 +08:00
|
|
|
|
2000-08-22 09:26:57 +08:00
|
|
|
case GIMP_GRAY_IMAGE:
|
2000-01-26 01:46:56 +08:00
|
|
|
save_8 (fp, width, height, pixels);
|
|
|
|
fputc (0x0c, fp);
|
2003-11-06 23:27:05 +08:00
|
|
|
for (i = 0; i < 256; i++)
|
2008-05-11 02:16:33 +08:00
|
|
|
{
|
|
|
|
fputc ((guchar) i, fp);
|
2007-08-30 19:49:52 +08:00
|
|
|
fputc ((guchar) i, fp);
|
|
|
|
fputc ((guchar) i, fp);
|
2008-05-11 02:16:33 +08:00
|
|
|
}
|
1998-01-25 09:24:46 +08:00
|
|
|
break;
|
2003-06-13 22:37:00 +08:00
|
|
|
|
1998-01-25 09:24:46 +08:00
|
|
|
default:
|
2000-01-26 01:46:56 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2012-11-20 05:26:04 +08:00
|
|
|
g_object_unref (buffer);
|
2000-01-26 01:46:56 +08:00
|
|
|
g_free (pixels);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2007-08-30 19:49:52 +08:00
|
|
|
if (fclose (fp) != 0)
|
|
|
|
{
|
2008-08-18 14:31:03 +08:00
|
|
|
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
|
|
|
|
_("Writing to file '%s' failed: %s"),
|
|
|
|
gimp_filename_to_utf8 (filename), g_strerror (errno));
|
2007-08-30 19:49:52 +08:00
|
|
|
return FALSE;
|
|
|
|
}
|
2008-08-18 14:31:03 +08:00
|
|
|
|
1998-01-25 09:24:46 +08:00
|
|
|
return TRUE;
|
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
static void
|
2009-02-25 18:56:41 +08:00
|
|
|
save_8 (FILE *fp,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
2012-11-20 05:26:04 +08:00
|
|
|
const guchar *buf)
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
1998-01-25 09:24:46 +08:00
|
|
|
int row;
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
for (row = 0; row < height; ++row)
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
2012-11-20 05:26:04 +08:00
|
|
|
writeline (fp, buf, width);
|
|
|
|
buf += width;
|
2000-01-26 01:46:56 +08:00
|
|
|
gimp_progress_update ((double) row / (double) height);
|
1999-10-10 03:06:14 +08:00
|
|
|
}
|
1998-01-25 09:24:46 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
static void
|
2009-02-25 18:56:41 +08:00
|
|
|
save_24 (FILE *fp,
|
|
|
|
gint width,
|
|
|
|
gint height,
|
2012-11-20 05:26:04 +08:00
|
|
|
const guchar *buf)
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
2007-08-30 19:49:52 +08:00
|
|
|
int x, y, c;
|
1998-01-25 09:24:46 +08:00
|
|
|
guchar *line;
|
2007-08-30 19:49:52 +08:00
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
line = (guchar *) g_malloc (width);
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
for (y = 0; y < height; ++y)
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
2003-11-06 23:27:05 +08:00
|
|
|
for (c = 0; c < 3; ++c)
|
2008-05-11 02:16:33 +08:00
|
|
|
{
|
|
|
|
for (x = 0; x < width; ++x)
|
|
|
|
{
|
2012-11-20 05:26:04 +08:00
|
|
|
line[x] = buf[(3*x) + c];
|
2008-05-11 02:16:33 +08:00
|
|
|
}
|
|
|
|
writeline (fp, line, width);
|
|
|
|
}
|
2012-11-20 05:26:04 +08:00
|
|
|
buf += width * 3;
|
2007-08-30 19:49:52 +08:00
|
|
|
gimp_progress_update ((double) y / (double) height);
|
1999-10-10 03:06:14 +08:00
|
|
|
}
|
1998-01-25 09:24:46 +08:00
|
|
|
g_free (line);
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|
|
|
|
|
2000-01-26 01:46:56 +08:00
|
|
|
static void
|
2009-02-25 18:56:41 +08:00
|
|
|
writeline (FILE *fp,
|
2012-11-20 05:26:04 +08:00
|
|
|
const guchar *buf,
|
2009-02-25 18:56:41 +08:00
|
|
|
gint bytes)
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
2012-11-20 05:26:04 +08:00
|
|
|
const guchar *finish = buf + bytes;
|
2009-02-25 18:56:41 +08:00
|
|
|
guchar value;
|
|
|
|
guchar count;
|
1998-01-25 09:24:46 +08:00
|
|
|
|
2012-11-20 05:26:04 +08:00
|
|
|
while (buf < finish)
|
1999-10-10 03:06:14 +08:00
|
|
|
{
|
2012-11-20 05:26:04 +08:00
|
|
|
value = *(buf++);
|
2000-01-26 01:46:56 +08:00
|
|
|
count = 1;
|
2003-11-06 23:27:05 +08:00
|
|
|
|
2012-11-20 05:26:04 +08:00
|
|
|
while (buf < finish && count < 63 && *buf == value)
|
2008-05-11 02:16:33 +08:00
|
|
|
{
|
2012-11-20 05:26:04 +08:00
|
|
|
count++; buf++;
|
2008-05-11 02:16:33 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
|
2003-11-06 23:27:05 +08:00
|
|
|
if (value < 0xc0 && count == 1)
|
2008-05-11 02:16:33 +08:00
|
|
|
{
|
|
|
|
fputc (value, fp);
|
|
|
|
}
|
2003-11-06 23:27:05 +08:00
|
|
|
else
|
2008-05-11 02:16:33 +08:00
|
|
|
{
|
|
|
|
fputc (0xc0 + count, fp);
|
|
|
|
fputc (value, fp);
|
|
|
|
}
|
1998-01-25 09:24:46 +08:00
|
|
|
}
|
1997-11-25 06:05:25 +08:00
|
|
|
}
|