Commit 93bfd6f1 authored by Michael Natterer's avatar Michael Natterer 😴

plug-ins: port file-aa to GimpProcedureConfig

Now takes an integer index into the aa_formats array instead of the
format's name. "4" is about as magic as "weird aallib format name #4"
so no harm done.
parent 4be4d828
Pipeline #121687 passed with stages
in 21 minutes and 57 seconds
/**
* aa.c version 1.0
* A plugin that uses libaa (ftp://ftp.ta.jcu.cz/pub/aa) to save images as
* ASCII.
* NOTE: This plugin *requires* aalib 1.2 or later. Earlier versions will
* not work.
* Code copied from all over the GIMP source.
* Tim Newsome <nuisance@cmu.edu>
*/
/* GIMP - The GNU Image Manipulation Program /* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis * Copyright (C) 1995 Spencer Kimball and Peter Mattis
* *
...@@ -25,6 +15,16 @@ ...@@ -25,6 +15,16 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
/**
* aa.c version 1.0
* A plugin that uses libaa (ftp://ftp.ta.jcu.cz/pub/aa) to save images as
* ASCII.
* NOTE: This plugin *requires* aalib 1.2 or later. Earlier versions will
* not work.
* Code copied from all over the GIMP source.
* Tim Newsome <nuisance@cmu.edu>
*/
#include "config.h" #include "config.h"
#include <string.h> #include <string.h>
...@@ -39,7 +39,6 @@ ...@@ -39,7 +39,6 @@
#define SAVE_PROC "file-aa-save" #define SAVE_PROC "file-aa-save"
#define PLUG_IN_BINARY "file-aa" #define PLUG_IN_BINARY "file-aa"
#define PLUG_IN_ROLE "gimp-file-aa"
typedef struct _Ascii Ascii; typedef struct _Ascii Ascii;
...@@ -63,23 +62,25 @@ GType ascii_get_type (void) G_GNUC_CONST; ...@@ -63,23 +62,25 @@ GType ascii_get_type (void) G_GNUC_CONST;
static GList * ascii_query_procedures (GimpPlugIn *plug_in); static GList * ascii_query_procedures (GimpPlugIn *plug_in);
static GimpProcedure * ascii_create_procedure (GimpPlugIn *plug_in, static GimpProcedure * ascii_create_procedure (GimpPlugIn *plug_in,
const gchar *name); const gchar *name);
static GimpValueArray * ascii_save (GimpProcedure *procedure, static GimpValueArray * ascii_save (GimpProcedure *procedure,
GimpRunMode run_mode, GimpRunMode run_mode,
GimpImage *image, GimpImage *image,
GimpDrawable *drawable, GimpDrawable *drawable,
GFile *file, GFile *file,
const GimpValueArray *args, const GimpValueArray *args,
gpointer run_data); gpointer run_data);
static gboolean save_aa (GimpDrawable *drawable, static gboolean save_aa (GFile *file,
gchar *filename, GimpDrawable *drawable,
gint output_type); GObject *config,
static void gimp2aa (GimpDrawable *drawable, GError **error);
aa_context *context); static void gimp2aa (GimpDrawable *drawable,
aa_context *context);
static gint aa_dialog (gint selected); static gboolean save_dialog (GimpProcedure *procedure,
GObject *config);
G_DEFINE_TYPE (Ascii, ascii, GIMP_TYPE_PLUG_IN) G_DEFINE_TYPE (Ascii, ascii, GIMP_TYPE_PLUG_IN)
...@@ -115,6 +116,8 @@ ascii_create_procedure (GimpPlugIn *plug_in, ...@@ -115,6 +116,8 @@ ascii_create_procedure (GimpPlugIn *plug_in,
if (! strcmp (name, SAVE_PROC)) if (! strcmp (name, SAVE_PROC))
{ {
gint i;
procedure = gimp_save_procedure_new (plug_in, name, procedure = gimp_save_procedure_new (plug_in, name,
GIMP_PDB_PROC_TYPE_PLUGIN, GIMP_PDB_PROC_TYPE_PLUGIN,
ascii_save, NULL, NULL); ascii_save, NULL, NULL);
...@@ -140,37 +143,16 @@ ascii_create_procedure (GimpPlugIn *plug_in, ...@@ -140,37 +143,16 @@ ascii_create_procedure (GimpPlugIn *plug_in,
gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure), gimp_file_procedure_set_extensions (GIMP_FILE_PROCEDURE (procedure),
"txt,ansi,text"); "txt,ansi,text");
GIMP_PROC_ARG_STRING (procedure, "file-type", for (i = 0; aa_formats[i]; i++);
"File type",
"File type to use",
NULL,
G_PARAM_READWRITE);
}
return procedure;
}
/**
* Searches aa_formats defined by aalib to find the index of the type
* specified by string.
* -1 means it wasn't found.
*/
static gint
get_type_from_string (const gchar *string)
{
gint type = 0;
aa_format **p = (aa_format **) aa_formats;
while (*p && strcmp ((*p)->formatname, string)) GIMP_PROC_ARG_INT (procedure, "file-type",
{ "File type",
p++; "File type to use",
type++; 0, i, 0,
G_PARAM_READWRITE);
} }
if (*p == NULL) return procedure;
return -1;
return type;
} }
static GimpValueArray * static GimpValueArray *
...@@ -182,14 +164,17 @@ ascii_save (GimpProcedure *procedure, ...@@ -182,14 +164,17 @@ ascii_save (GimpProcedure *procedure,
const GimpValueArray *args, const GimpValueArray *args,
gpointer run_data) gpointer run_data)
{ {
GimpPDBStatusType status = GIMP_PDB_SUCCESS; GimpProcedureConfig *config;
GimpExportReturn export = GIMP_EXPORT_CANCEL; GimpPDBStatusType status = GIMP_PDB_SUCCESS;
gint output_type = 0; GimpExportReturn export = GIMP_EXPORT_CANCEL;
GError *error = NULL; GError *error = NULL;
INIT_I18N (); INIT_I18N ();
gegl_init (NULL, NULL); gegl_init (NULL, NULL);
config = gimp_procedure_create_config (procedure);
gimp_procedure_config_begin_run (config, image, run_mode, args);
switch (run_mode) switch (run_mode)
{ {
case GIMP_RUN_INTERACTIVE: case GIMP_RUN_INTERACTIVE:
...@@ -212,72 +197,59 @@ ascii_save (GimpProcedure *procedure, ...@@ -212,72 +197,59 @@ ascii_save (GimpProcedure *procedure,
break; break;
} }
switch (run_mode) if (run_mode == GIMP_RUN_INTERACTIVE)
{ {
case GIMP_RUN_INTERACTIVE: if (! save_dialog (procedure, G_OBJECT (config)))
gimp_get_data (SAVE_PROC, &output_type);
output_type = aa_dialog (output_type);
if (output_type < 0)
status = GIMP_PDB_CANCEL; status = GIMP_PDB_CANCEL;
break;
case GIMP_RUN_NONINTERACTIVE:
output_type = get_type_from_string (GIMP_VALUES_GET_STRING (args, 0));
if (output_type < 0)
status = GIMP_PDB_CALLING_ERROR;
break;
case GIMP_RUN_WITH_LAST_VALS:
gimp_get_data (SAVE_PROC, &output_type);
break;
default:
break;
} }
if (status == GIMP_PDB_SUCCESS) if (status == GIMP_PDB_SUCCESS)
{ {
if (save_aa (drawable, g_file_get_path (file), output_type)) if (! save_aa (file, drawable, G_OBJECT (config), &error))
{
gimp_set_data (SAVE_PROC, &output_type, sizeof (output_type));
}
else
{ {
status = GIMP_PDB_EXECUTION_ERROR; status = GIMP_PDB_EXECUTION_ERROR;
} }
} }
gimp_procedure_config_end_run (config, status);
g_object_unref (config);
if (export == GIMP_EXPORT_EXPORT) if (export == GIMP_EXPORT_EXPORT)
gimp_image_delete (image); gimp_image_delete (image);
return gimp_procedure_new_return_values (procedure, status, error); return gimp_procedure_new_return_values (procedure, status, error);
} }
/**
* The actual save function. What it's all about.
* The image type has to be GRAY.
*/
static gboolean static gboolean
save_aa (GimpDrawable *drawable, save_aa (GFile *file,
gchar *filename, GimpDrawable *drawable,
gint output_type) GObject *config,
GError **error)
{ {
aa_savedata savedata; aa_savedata savedata;
aa_context *context; aa_context *context;
aa_format format = *aa_formats[output_type]; aa_format format;
gint output_type;
g_object_get (config,
"file-type", &output_type,
NULL);
memcpy (&format, aa_formats[output_type], sizeof (aa_format));
format.width = gimp_drawable_width (drawable) / 2; format.width = gimp_drawable_width (drawable) / 2;
format.height = gimp_drawable_height (drawable) / 2; format.height = gimp_drawable_height (drawable) / 2;
/* Get a libaa context which will save its output to filename. */ /* Get a libaa context which will save its output to filename. */
savedata.name = filename; savedata.name = g_file_get_path (file);
savedata.format = &format; savedata.format = &format;
context = aa_init (&save_d, &aa_defparams, &savedata); context = aa_init (&save_d, &aa_defparams, &savedata);
if (!context) if (! context)
return FALSE; return FALSE;
gimp2aa (drawable, context); gimp2aa (drawable, context);
aa_flush (context); aa_flush (context);
aa_close (context); aa_close (context);
...@@ -380,17 +352,21 @@ gimp2aa (GimpDrawable *drawable, ...@@ -380,17 +352,21 @@ gimp2aa (GimpDrawable *drawable,
aa_scrwidth (context), aa_scrheight (context)); aa_scrwidth (context), aa_scrheight (context));
} }
static gint static gboolean
aa_dialog (gint selected) save_dialog (GimpProcedure *procedure,
GObject *config)
{ {
GtkWidget *dialog; GtkWidget *dialog;
GtkWidget *hbox; GtkWidget *hbox;
GtkWidget *label; GtkWidget *label;
GtkWidget *combo; GtkListStore *store;
gint i; GtkWidget *combo;
gint i;
/* Create the actual window. */ gboolean run;
dialog = gimp_export_dialog_new (_("Text"), PLUG_IN_BINARY, SAVE_PROC);
dialog = gimp_procedure_dialog_new (procedure,
GIMP_PROCEDURE_CONFIG (config),
_("Export Image as Text"));
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6); hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
gtk_container_set_border_width (GTK_CONTAINER (hbox), 12); gtk_container_set_border_width (GTK_CONTAINER (hbox), 12);
...@@ -402,28 +378,23 @@ aa_dialog (gint selected) ...@@ -402,28 +378,23 @@ aa_dialog (gint selected)
gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
gtk_widget_show (label); gtk_widget_show (label);
combo = g_object_new (GIMP_TYPE_INT_COMBO_BOX, NULL); store = g_object_new (GIMP_TYPE_INT_STORE, NULL);
gtk_box_pack_start (GTK_BOX (hbox), combo, TRUE, TRUE, 0);
gtk_widget_show (combo);
gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
for (i = 0; aa_formats[i]; i++) for (i = 0; aa_formats[i]; i++)
gimp_int_combo_box_append (GIMP_INT_COMBO_BOX (combo), gtk_list_store_insert_with_values (store, NULL, -1,
GIMP_INT_STORE_VALUE, i, GIMP_INT_STORE_VALUE, i,
GIMP_INT_STORE_LABEL, aa_formats[i]->formatname, GIMP_INT_STORE_LABEL, aa_formats[i]->formatname,
-1); -1);
gimp_int_combo_box_connect (GIMP_INT_COMBO_BOX (combo), selected, combo = gimp_prop_int_combo_box_new (config, "file-type",
G_CALLBACK (gimp_int_combo_box_get_active), GIMP_INT_STORE (store));
&selected, NULL); gtk_box_pack_start (GTK_BOX (hbox), combo, TRUE, TRUE, 0);
gtk_widget_show (dialog); gtk_widget_show (dialog);
if (gimp_dialog_run (GIMP_DIALOG (dialog)) != GTK_RESPONSE_OK) run = gimp_procedure_dialog_run (GIMP_PROCEDURE_DIALOG (dialog));
selected = -1;
gtk_widget_destroy (dialog); gtk_widget_destroy (dialog);
return selected; return run;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment