plug-ins: port file-tga to GimpPlugIn and libgimp objects

This commit is contained in:
Michael Natterer 2019-08-24 18:34:53 +02:00
parent 9847072978
commit 0ae833b0e0
3 changed files with 285 additions and 289 deletions

View file

@ -1108,8 +1108,6 @@ file_svg_LDADD = \
$(INTLLIBS) \
$(file_svg_RC)
file_tga_CPPFLAGS = $(AM_CPPFLAGS) -DGIMP_DEPRECATED_REPLACE_NEW_API
file_tga_SOURCES = \
file-tga.c

View file

@ -72,15 +72,8 @@
* one fully transparent color is allowed in INDEXEDA mode).
*/
/* Set these for debugging. */
/* #define PROFILE 1 */
#include "config.h"
#ifdef PROFILE
# include <sys/times.h>
#endif
#include <errno.h>
#include <string.h>
@ -109,20 +102,6 @@ typedef struct _TgaSaveVals
TgaOrigin origin;
} TgaSaveVals;
static TgaSaveVals tsvals =
{
TRUE, /* rle */
ORIGIN_BOTTOM_LEFT /* origin */
};
/* TRUEVISION-XFILE magic signature string */
static guchar magic[18] =
{
0x54, 0x52, 0x55, 0x45, 0x56, 0x49, 0x53, 0x49, 0x4f,
0x4e, 0x2d, 0x58, 0x46, 0x49, 0x4c, 0x45, 0x2e, 0x0
};
typedef struct tga_info_struct
{
guint8 idLength;
@ -176,269 +155,288 @@ typedef struct tga_info_struct
} tga_info;
/* Declare some local functions.
*/
static void query (void);
static void run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals);
typedef struct _Tga Tga;
typedef struct _TgaClass TgaClass;
static gint32 load_image (const gchar *filename,
GError **error);
static gint save_image (const gchar *filename,
gint32 image_ID,
gint32 drawable_ID,
GError **error);
static gboolean save_dialog (void);
static gint32 ReadImage (FILE *fp,
tga_info *info,
const gchar *filename);
const GimpPlugInInfo PLUG_IN_INFO =
struct _Tga
{
NULL, /* init_proc */
NULL, /* quit_proc */
query, /* query_proc */
run, /* run_proc */
GimpPlugIn parent_instance;
};
struct _TgaClass
{
GimpPlugInClass parent_class;
};
MAIN ()
#define TGA_TYPE (tga_get_type ())
#define TGA (obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TGA_TYPE, Tga))
GType tga_get_type (void) G_GNUC_CONST;
static GList * tga_query_procedures (GimpPlugIn *plug_in);
static GimpProcedure * tga_create_procedure (GimpPlugIn *plug_in,
const gchar *name);
static GimpValueArray * tga_load (GimpProcedure *procedure,
GimpRunMode run_mode,
GFile *file,
const GimpValueArray *args,
gpointer run_data);
static GimpValueArray * tga_save (GimpProcedure *procedure,
GimpRunMode run_mode,
GimpImage *image,
GimpDrawable *drawable,
GFile *file,
const GimpValueArray *args,
gpointer run_data);
static GimpImage * load_image (const gchar *filename,
GError **error);
static gboolean save_image (const gchar *filename,
GimpImage *image,
GimpDrawable *drawable,
GError **error);
static gboolean save_dialog (void);
static GimpImage * ReadImage (FILE *fp,
tga_info *info,
const gchar *filename);
G_DEFINE_TYPE (Tga, tga, GIMP_TYPE_PLUG_IN)
GIMP_MAIN (TGA_TYPE)
static TgaSaveVals tsvals =
{
TRUE, /* rle */
ORIGIN_BOTTOM_LEFT /* origin */
};
/* TRUEVISION-XFILE magic signature string */
static guchar magic[18] =
{
0x54, 0x52, 0x55, 0x45, 0x56, 0x49, 0x53, 0x49, 0x4f,
0x4e, 0x2d, 0x58, 0x46, 0x49, 0x4c, 0x45, 0x2e, 0x0
};
static void
query (void)
tga_class_init (TgaClass *klass)
{
static const GimpParamDef load_args[] =
{
{ GIMP_PDB_INT32, "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
{ GIMP_PDB_STRING, "filename", "The name of the file to load" },
{ GIMP_PDB_STRING, "raw-filename", "The name entered" }
};
GimpPlugInClass *plug_in_class = GIMP_PLUG_IN_CLASS (klass);
static const GimpParamDef load_return_vals[] =
{
{ GIMP_PDB_IMAGE, "image", "Output image" }
};
static const GimpParamDef save_args[] =
{
{ GIMP_PDB_INT32, "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
{ GIMP_PDB_IMAGE, "image", "Input image" },
{ GIMP_PDB_DRAWABLE, "drawable", "Drawable to export" },
{ GIMP_PDB_STRING, "filename", "The name of the file to export the image in" },
{ GIMP_PDB_STRING, "raw-filename", "The name of the file to export the image in" },
{ GIMP_PDB_INT32, "rle", "Use RLE compression" },
{ GIMP_PDB_INT32, "origin", "Image origin (0 = top-left, 1 = bottom-left)"}
} ;
gimp_install_procedure (LOAD_PROC,
"Loads files of Targa file format",
"FIXME: write help for tga_load",
"Raphael FRANCOIS, Gordon Matzigkeit",
"Raphael FRANCOIS, Gordon Matzigkeit",
"1997,2000,2007",
N_("TarGA image"),
NULL,
GIMP_PLUGIN,
G_N_ELEMENTS (load_args),
G_N_ELEMENTS (load_return_vals),
load_args, load_return_vals);
gimp_register_file_handler_mime (LOAD_PROC, "image/x-tga");
gimp_register_magic_load_handler (LOAD_PROC,
"tga,vda,icb,vst",
"",
"-18&,string,TRUEVISION-XFILE.,-1,byte,0");
gimp_install_procedure (SAVE_PROC,
"exports files in the Targa file format",
"FIXME: write help for tga_save",
"Raphael FRANCOIS, Gordon Matzigkeit",
"Raphael FRANCOIS, Gordon Matzigkeit",
"1997,2000",
N_("TarGA image"),
"RGB*, GRAY*, INDEXED*",
GIMP_PLUGIN,
G_N_ELEMENTS (save_args), 0,
save_args, NULL);
gimp_register_file_handler_mime (SAVE_PROC, "image/x-tga");
gimp_register_save_handler (SAVE_PROC, "tga", "");
plug_in_class->query_procedures = tga_query_procedures;
plug_in_class->create_procedure = tga_create_procedure;
}
static void
run (const gchar *name,
gint nparams,
const GimpParam *param,
gint *nreturn_vals,
GimpParam **return_vals)
tga_init (Tga *tga)
{
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;
}
#ifdef PROFILE
struct tms tbuf1, tbuf2;
#endif
static GList *
tga_query_procedures (GimpPlugIn *plug_in)
{
GList *list = NULL;
list = g_list_append (list, g_strdup (LOAD_PROC));
list = g_list_append (list, g_strdup (SAVE_PROC));
return list;
}
static GimpProcedure *
tga_create_procedure (GimpPlugIn *plug_in,
const gchar *name)
{
GimpProcedure *procedure = NULL;
if (! strcmp (name, LOAD_PROC))
{
procedure = gimp_load_procedure_new (plug_in, name, GIMP_PLUGIN,
tga_load, NULL, NULL);
gimp_procedure_set_menu_label (procedure, N_("TarGA image"));
gimp_procedure_set_documentation (procedure,
"Loads files of Targa file format",
"FIXME: write help for tga_load",
name);
gimp_procedure_set_attribution (procedure,
"Raphael FRANCOIS, Gordon Matzigkeit",
"Raphael FRANCOIS, Gordon Matzigkeit",
"1997,2000,2007");
gimp_file_procedure_set_mime_types (GIMP_FILE_PROCEDURE (procedure),
"image/x-tga");
gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
"tga,vda,icb,vst");
gimp_file_procedure_set_magics (GIMP_FILE_PROCEDURE (procedure),
"-18&,string,TRUEVISION-XFILE.,-1,byte,0");
}
else if (! strcmp (name, SAVE_PROC))
{
procedure = gimp_save_procedure_new (plug_in, name, GIMP_PLUGIN,
tga_save, NULL, NULL);
gimp_procedure_set_image_types (procedure, "*");
gimp_procedure_set_menu_label (procedure, N_("TarGA image"));
gimp_procedure_set_documentation (procedure,
"Exports files in the Targa file format",
"FIXME: write help for tga_save",
name);
gimp_procedure_set_attribution (procedure,
"Raphael FRANCOIS, Gordon Matzigkeit",
"Raphael FRANCOIS, Gordon Matzigkeit",
"1997,2000");
gimp_file_procedure_set_mime_types (GIMP_FILE_PROCEDURE (procedure),
"image/x-tga");
gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
"tga");
GIMP_PROC_ARG_BOOLEAN (procedure, "rle",
"RLE",
"Use RLE compression",
TRUE,
G_PARAM_READWRITE);
GIMP_PROC_ARG_INT (procedure, "origin",
"Origin",
"Image origin (0 = top-left, 1 = bottom-left)",
0, 1, ORIGIN_BOTTOM_LEFT,
G_PARAM_READWRITE);
}
return procedure;
}
static GimpValueArray *
tga_load (GimpProcedure *procedure,
GimpRunMode run_mode,
GFile *file,
const GimpValueArray *args,
gpointer run_data)
{
GimpValueArray *return_vals;
GimpImage *image;
GError *error = NULL;
INIT_I18N ();
gegl_init (NULL, NULL);
run_mode = param[0].data.d_int32;
image = load_image (g_file_get_path (file), &error);
*nreturn_vals = 1;
*return_vals = values;
if (! image)
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_EXECUTION_ERROR,
error);
values[0].type = GIMP_PDB_STATUS;
values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
return_vals = gimp_procedure_new_return_values (procedure,
GIMP_PDB_SUCCESS,
NULL);
if (strcmp (name, LOAD_PROC) == 0)
GIMP_VALUES_SET_IMAGE (return_vals, 1, image);
return return_vals;
}
static GimpValueArray *
tga_save (GimpProcedure *procedure,
GimpRunMode run_mode,
GimpImage *image,
GimpDrawable *drawable,
GFile *file,
const GimpValueArray *args,
gpointer run_data)
{
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
GimpExportReturn export = GIMP_EXPORT_CANCEL;
GError *error = NULL;
INIT_I18N ();
gegl_init (NULL, NULL);
switch (run_mode)
{
GFile *file = g_file_new_for_uri (param[1].data.d_string);
case GIMP_RUN_INTERACTIVE:
case GIMP_RUN_WITH_LAST_VALS:
gimp_ui_init (PLUG_IN_BINARY, FALSE);
#ifdef PROFILE
times (&tbuf1);
#endif
export = gimp_export_image (&image, &drawable, "TGA",
GIMP_EXPORT_CAN_HANDLE_RGB |
GIMP_EXPORT_CAN_HANDLE_GRAY |
GIMP_EXPORT_CAN_HANDLE_INDEXED |
GIMP_EXPORT_CAN_HANDLE_ALPHA);
image_ID = load_image (g_file_get_path (file), &error);
if (export == GIMP_EXPORT_CANCEL)
return gimp_procedure_new_return_values (procedure,
GIMP_PDB_CANCEL,
NULL);
break;
if (image_ID != -1)
default:
break;
}
switch (run_mode)
{
case GIMP_RUN_INTERACTIVE:
gimp_get_data (SAVE_PROC, &tsvals);
if (! save_dialog ())
status = GIMP_PDB_CANCEL;
break;
case GIMP_RUN_NONINTERACTIVE:
tsvals.rle = GIMP_VALUES_GET_BOOLEAN (args, 0);
break;
case GIMP_RUN_WITH_LAST_VALS:
gimp_get_data (SAVE_PROC, &tsvals);
break;
default:
break;
}
if (status == GIMP_PDB_SUCCESS)
{
if (save_image (g_file_get_path (file),
image, drawable,
&error))
{
*nreturn_vals = 2;
values[1].type = GIMP_PDB_IMAGE;
values[1].data.d_image = image_ID;
gimp_set_data (SAVE_PROC, &tsvals, sizeof (tsvals));
}
else
{
status = GIMP_PDB_EXECUTION_ERROR;
}
}
else if (strcmp (name, SAVE_PROC) == 0)
{
GFile *file = g_file_new_for_uri (param[3].data.d_string);
gimp_ui_init (PLUG_IN_BINARY, FALSE);
if (export == GIMP_EXPORT_EXPORT)
gimp_image_delete (image);
image_ID = param[1].data.d_int32;
drawable_ID = param[2].data.d_int32;
/* eventually export the image */
switch (run_mode)
{
case GIMP_RUN_INTERACTIVE:
case GIMP_RUN_WITH_LAST_VALS:
export = gimp_export_image (&image_ID, &drawable_ID, "TGA",
GIMP_EXPORT_CAN_HANDLE_RGB |
GIMP_EXPORT_CAN_HANDLE_GRAY |
GIMP_EXPORT_CAN_HANDLE_INDEXED |
GIMP_EXPORT_CAN_HANDLE_ALPHA);
if (export == GIMP_EXPORT_CANCEL)
{
values[0].data.d_status = GIMP_PDB_CANCEL;
return;
}
break;
default:
break;
}
switch (run_mode)
{
case GIMP_RUN_INTERACTIVE:
/* Possibly retrieve data */
gimp_get_data (SAVE_PROC, &tsvals);
/* First acquire information with a dialog */
if (! save_dialog ())
status = GIMP_PDB_CANCEL;
break;
case GIMP_RUN_NONINTERACTIVE:
/* Make sure all the arguments are there! */
if (nparams != 7)
{
status = GIMP_PDB_CALLING_ERROR;
}
else
{
tsvals.rle = (param[5].data.d_int32) ? TRUE : FALSE;
}
break;
case GIMP_RUN_WITH_LAST_VALS:
/* Possibly retrieve data */
gimp_get_data (SAVE_PROC, &tsvals);
break;
default:
break;
}
#ifdef PROFILE
times (&tbuf1);
#endif
if (status == GIMP_PDB_SUCCESS)
{
if (save_image (g_file_get_path (file),
image_ID, drawable_ID,
&error))
{
/* Store psvals data */
gimp_set_data (SAVE_PROC, &tsvals, sizeof (tsvals));
}
else
{
status = GIMP_PDB_EXECUTION_ERROR;
}
}
if (export == GIMP_EXPORT_EXPORT)
gimp_image_delete (image_ID);
}
else
{
status = GIMP_PDB_CALLING_ERROR;
}
if (status != GIMP_PDB_SUCCESS && error)
{
*nreturn_vals = 2;
values[1].type = GIMP_PDB_STRING;
values[1].data.d_string = error->message;
}
values[0].data.d_status = status;
#ifdef PROFILE
times (&tbuf2);
printf ("TGA: %s profile: %ld user %ld system\n", name,
(long) tbuf2.tms_utime - tbuf1.tms_utime,
(long) tbuf2.tms_stime - tbuf2.tms_stime);
#endif
return gimp_procedure_new_return_values (procedure, status, error);
}
static gint32
static GimpImage *
load_image (const gchar *filename,
GError **error)
{
FILE *fp;
tga_info info;
guchar header[18];
guchar footer[26];
guchar extension[495];
long offset;
gint32 image_ID = -1;
FILE *fp;
tga_info info;
guchar header[18];
guchar footer[26];
guchar extension[495];
long offset;
GimpImage *image = NULL;
gimp_progress_init_printf (_("Opening '%s'"),
gimp_filename_to_utf8 (filename));
@ -450,7 +448,7 @@ load_image (const gchar *filename,
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));
return -1;
return NULL;
}
/* Is file big enough for a footer? */
@ -460,7 +458,7 @@ load_image (const gchar *filename,
{
g_message (_("Cannot read footer from '%s'"),
gimp_filename_to_utf8 (filename));
return -1;
return NULL;
}
else if (memcmp (footer + 8, magic, sizeof (magic)) == 0)
{
@ -478,7 +476,7 @@ load_image (const gchar *filename,
{
g_message (_("Cannot read extension from '%s'"),
gimp_filename_to_utf8 (filename));
return -1;
return NULL;
}
/* Eventually actually handle version 2 TGA here */
}
@ -490,7 +488,7 @@ load_image (const gchar *filename,
{
g_message (_("Cannot read header from '%s'"),
gimp_filename_to_utf8 (filename));
return -1;
return NULL;
}
switch (header[2])
@ -565,7 +563,7 @@ load_image (const gchar *filename,
g_message ("Unhandled sub-format in '%s' (type = %u, bpp = %u)",
gimp_filename_to_utf8 (filename),
info.imageType, info.bpp);
return -1;
return NULL;
}
break;
case TGA_TYPE_COLOR:
@ -580,7 +578,7 @@ load_image (const gchar *filename,
g_message ("Unhandled sub-format in '%s' (type = %u, bpp = %u, alpha = %u)",
gimp_filename_to_utf8 (filename),
info.imageType, info.bpp, info.alphaBits);
return -1;
return NULL;
}
break;
case TGA_TYPE_GRAY:
@ -590,14 +588,14 @@ load_image (const gchar *filename,
g_message ("Unhandled sub-format in '%s' (type = %u, bpp = %u)",
gimp_filename_to_utf8 (filename),
info.imageType, info.bpp);
return -1;
return NULL;
}
break;
default:
g_message ("Unknown image type %u for '%s'",
info.imageType, gimp_filename_to_utf8 (filename));
return -1;
return NULL;
}
/* Plausible but unhandled formats */
@ -606,7 +604,7 @@ load_image (const gchar *filename,
g_message ("Unhandled sub-format in '%s' (type = %u, bpp = %u)",
gimp_filename_to_utf8 (filename),
info.imageType, info.bpp);
return -1;
return NULL;
}
/* Check that we have a color map only when we need it. */
@ -614,13 +612,13 @@ load_image (const gchar *filename,
{
g_message ("Indexed image has invalid color map type %u",
info.colorMapType);
return -1;
return NULL;
}
else if (info.imageType != TGA_TYPE_MAPPED && info.colorMapType != 0)
{
g_message ("Non-indexed image has invalid color map type %u",
info.colorMapType);
return -1;
return NULL;
}
/* Skip the image ID field. */
@ -628,14 +626,14 @@ load_image (const gchar *filename,
{
g_message ("File '%s' is truncated or corrupted",
gimp_filename_to_utf8 (filename));
return -1;
return NULL;
}
image_ID = ReadImage (fp, &info, filename);
image = ReadImage (fp, &info, filename);
fclose (fp);
return image_ID;
return image;
}
static void
@ -970,13 +968,13 @@ read_line (FILE *fp,
}
}
static gint32
static GimpImage *
ReadImage (FILE *fp,
tga_info *info,
const gchar *filename)
{
static gint32 image_ID;
gint32 layer_ID;
GimpImage *image;
GimpLayer *layer;
GeglBuffer *buffer;
guchar *data, *buf, *row;
GimpImageType dtype = 0;
@ -1065,7 +1063,7 @@ ReadImage (FILE *fp,
{
g_message ("Unsupported colormap depth: %u",
info->colorMapSize);
return -1;
return NULL;
}
}
else
@ -1080,7 +1078,7 @@ ReadImage (FILE *fp,
{
g_message ("Unsupported colormap depth: %u",
info->colorMapSize);
return -1;
return NULL;
}
}
}
@ -1088,28 +1086,28 @@ ReadImage (FILE *fp,
{
g_message ("File '%s' is truncated or corrupted",
gimp_filename_to_utf8 (filename));
return -1;
return NULL;
}
}
image_ID = gimp_image_new (info->width, info->height, itype);
gimp_image_set_filename (image_ID, filename);
image = gimp_image_new (info->width, info->height, itype);
gimp_image_set_filename (image, filename);
if (gimp_cmap)
gimp_image_set_colormap (image_ID, gimp_cmap, info->colorMapLength);
gimp_image_set_colormap (image, gimp_cmap, info->colorMapLength);
layer_ID = gimp_layer_new (image_ID,
_("Background"),
info->width, info->height,
dtype,
100,
gimp_image_get_default_new_layer_mode (image_ID));
layer = gimp_layer_new (image,
_("Background"),
info->width, info->height,
dtype,
100,
gimp_image_get_default_new_layer_mode (image));
gimp_image_insert_layer (image_ID, layer_ID, -1, 0);
gimp_image_insert_layer (image, layer, NULL, 0);
buffer = gimp_drawable_get_buffer (layer_ID);
buffer = gimp_drawable_get_buffer (GIMP_DRAWABLE (layer));
bpp = gimp_drawable_bpp (layer_ID);
bpp = gimp_drawable_bpp (GIMP_DRAWABLE (layer));
/* Allocate the data. */
max_tileheight = gimp_tile_height ();
@ -1171,15 +1169,15 @@ ReadImage (FILE *fp,
gimp_progress_update (1.0);
return image_ID;
return image;
}
static gboolean
save_image (const gchar *filename,
gint32 image_ID,
gint32 drawable_ID,
GError **error)
save_image (const gchar *filename,
GimpImage *image,
GimpDrawable *drawable,
GError **error)
{
GeglBuffer *buffer;
const Babl *format = NULL;
@ -1197,9 +1195,9 @@ save_image (const gchar *filename,
gint num_colors;
guchar *gimp_cmap = NULL;
buffer = gimp_drawable_get_buffer (drawable_ID);
buffer = gimp_drawable_get_buffer (drawable);
dtype = gimp_drawable_type (drawable_ID);
dtype = gimp_drawable_type (drawable);
width = gegl_buffer_get_width (buffer);
height = gegl_buffer_get_height (buffer);
@ -1219,7 +1217,7 @@ save_image (const gchar *filename,
if (dtype == GIMP_INDEXED_IMAGE)
{
gimp_cmap = gimp_image_get_colormap (image_ID, &num_colors);
gimp_cmap = gimp_image_get_colormap (image, &num_colors);
header[1] = 1; /* cmap type */
header[2] = (tsvals.rle) ? 9 : 1;
@ -1230,7 +1228,7 @@ save_image (const gchar *filename,
}
else if (dtype == GIMP_INDEXEDA_IMAGE)
{
gimp_cmap = gimp_image_get_colormap (image_ID, &num_colors);
gimp_cmap = gimp_image_get_colormap (image, &num_colors);
header[1] = 1; /* cmap type */
header[2] = (tsvals.rle) ? 9 : 1;

View file

@ -45,7 +45,7 @@
'file-raw-data' => { ui => 1, gegl => 1 },
'file-sunras' => { ui => 1, gegl => 1, old_api => 1 },
'file-svg' => { ui => 1, gegl => 1, libs => 'SVG_LIBS', cflags => 'SVG_CFLAGS' },
'file-tga' => { ui => 1, gegl => 1, old_api => 1 },
'file-tga' => { ui => 1, gegl => 1 },
'file-wmf' => { ui => 1, gegl => 1, optional => 1, libs => 'WMF_LIBS', cflags => 'WMF_CFLAGS' },
'file-xbm' => { ui => 1, gegl => 1 },
'file-xmc' => { ui => 1, gegl => 1, optional => 1, libs => 'XMC_LIBS' },