aa.c 9.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/**
 * 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>
 */

Shirasaki Yasuhiro's avatar
Shirasaki Yasuhiro committed
11 12
#include "config.h"

13 14
#include <stdio.h>
#include <stdlib.h>
15
#include <string.h>
16 17 18 19 20

#include <aalib.h>

#include <gtk/gtk.h>

21
#include <libgimp/gimp.h>
22
#include <libgimp/gimpui.h>
23

Shirasaki Yasuhiro's avatar
Shirasaki Yasuhiro committed
24 25
#include "libgimp/stdplugins-intl.h"

26 27 28
/* 
 * Declare some local functions.
 */
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
static void     query      (void);
static void     run        (gchar      *name, 
                            gint        nparams, 
                            GimpParam  *param, 
                            gint       *nreturn_vals, 
                            GimpParam **return_vals);
static gboolean save_aa    (gint32      drawable_ID,
                            gchar      *filename, 
                            gint        output_type);
static void     gimp2aa    (gint32      drawable_ID, 
                            aa_context *context);

static gint     type_dialog                 (gint       selected);
static void     type_dialog_toggle_update   (GtkWidget *widget, 
                                             gpointer   data);
static void     type_dialog_cancel_callback (GtkWidget *widget, 
                                             gpointer   data);
46 47 48 49 50

/* 
 * Some global variables.
 */

Sven Neumann's avatar
Sven Neumann committed
51
GimpPlugInInfo PLUG_IN_INFO =
52
{
53 54
  NULL,  /* init_proc  */
  NULL,  /* quit_proc  */
55
  query, /* query_proc */
56
  run,   /* run_proc   */
57 58 59
};

/**
60
 * Type the user selected. (Global for easier UI coding.)
61
 */
62
static gint selected_type = 0;
63 64


65 66
MAIN ()

67
static void 
68
query (void)
69
{
Sven Neumann's avatar
Sven Neumann committed
70
  static GimpParamDef save_args[] =
71
  {
Sven Neumann's avatar
Sven Neumann committed
72 73 74 75 76 77
    {GIMP_PDB_INT32,    "run_mode",     "Interactive, non-interactive"},
    {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"},
    {GIMP_PDB_STRING,   "file_type",    "File type to use"}
78 79
  };

80
  gimp_install_procedure ("file_aa_save",
81 82 83
			  "Saves grayscale image in various text formats",
			  "This plug-in uses aalib to save grayscale image "
                          "as ascii art into a variety of text formats",
84 85 86 87
			  "Tim Newsome <nuisance@cmu.edu>",
			  "Tim Newsome <nuisance@cmu.edu>",
			  "1997",
			  "<Save>/AA",
88
			  "GRAY",  /* FIXME: add support for other formats ? */
Sven Neumann's avatar
Sven Neumann committed
89
			  GIMP_PLUGIN,
90
			  G_N_ELEMENTS (save_args), 0,
91 92
			  save_args, NULL);

93
  gimp_register_save_handler ("file_aa_save",
Sven Neumann's avatar
Sven Neumann committed
94
			      "ansi,txt,text",
95
			      "");
96 97 98 99 100 101 102
}

/**
 * Searches aa_formats defined by aalib to find the index of the type
 * specified by string.
 * -1 means it wasn't found.
 */
103 104
static gint 
get_type_from_string (const gchar *string)
105
{
106
  gint type = 0;
107
  aa_format **p = (aa_format **) aa_formats;
108

109 110 111 112 113
  while (*p && strcmp ((*p)->formatname, string))
    {
      p++;
      type++;
    }
114 115 116

  if (*p == NULL)
    return -1;
117

118
  return type;
119 120
}

121
static void 
122 123 124 125 126
run (gchar       *name, 
     gint         nparams, 
     GimpParam   *param, 
     gint        *nreturn_vals,
     GimpParam  **return_vals)
127
{
128 129 130 131 132 133 134
  static GimpParam      values[2];
  GimpRunMode           run_mode;
  GimpPDBStatusType     status = GIMP_PDB_SUCCESS;
  gint                  output_type = 0;
  gint32                image_ID;
  gint32                drawable_ID;
  GimpExportReturnType  export = GIMP_EXPORT_CANCEL;
135 136 137

  /* Set us up to return a status. */
  *nreturn_vals = 1;
138
  *return_vals  = values;
Sven Neumann's avatar
Sven Neumann committed
139 140
  values[0].type          = GIMP_PDB_STATUS;
  values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
141 142 143
  run_mode    = param[0].data.d_int32;
  image_ID    = param[1].data.d_int32;
  drawable_ID = param[2].data.d_int32;
144

145 146 147
  /*  eventually export the image */ 
  switch (run_mode)
    {
Sven Neumann's avatar
Sven Neumann committed
148 149
    case GIMP_RUN_INTERACTIVE:
    case GIMP_RUN_WITH_LAST_VALS:
Shirasaki Yasuhiro's avatar
Shirasaki Yasuhiro committed
150
      INIT_I18N_UI();
151
      gimp_ui_init ("aa", FALSE);
152
      export = gimp_export_image (&image_ID, &drawable_ID, "AA", 
153 154 155
				  (GIMP_EXPORT_CAN_HANDLE_GRAY |
				   GIMP_EXPORT_CAN_HANDLE_ALPHA ));
      if (export == GIMP_EXPORT_CANCEL)
156
	{
Sven Neumann's avatar
Sven Neumann committed
157
	  values[0].data.d_status = GIMP_PDB_CANCEL;
158 159 160 161
	  return;
	}
      break;
    default:
Shirasaki Yasuhiro's avatar
Shirasaki Yasuhiro committed
162
      INIT_I18N();
163 164
      break;
    }
165

166
  if (!gimp_drawable_is_gray (drawable_ID)) 
167
    {
Sven Neumann's avatar
Sven Neumann committed
168
      status = GIMP_PDB_CALLING_ERROR;
169
    }
170

Sven Neumann's avatar
Sven Neumann committed
171
  if (status == GIMP_PDB_SUCCESS)
172
    {
173 174
      switch (run_mode) 
	{
Sven Neumann's avatar
Sven Neumann committed
175
	case GIMP_RUN_INTERACTIVE:
176 177
	  gimp_get_data ("file_aa_save", &output_type);
	  output_type = type_dialog (output_type);
178
	  if (output_type < 0)
Sven Neumann's avatar
Sven Neumann committed
179
	    status = GIMP_PDB_CANCEL;
180 181
	  break;

Sven Neumann's avatar
Sven Neumann committed
182
	case GIMP_RUN_NONINTERACTIVE:
183 184 185
	  /*  Make sure all the arguments are there!  */
	  if (nparams != 6)
	    {
Sven Neumann's avatar
Sven Neumann committed
186
	      status = GIMP_PDB_CALLING_ERROR;
187 188 189 190 191
	    }
	  else
	    {
	      output_type = get_type_from_string (param[5].data.d_string);
	      if (output_type < 0)
Sven Neumann's avatar
Sven Neumann committed
192
		status = GIMP_PDB_CALLING_ERROR;
193 194 195
	    }
	  break;

Sven Neumann's avatar
Sven Neumann committed
196
	case GIMP_RUN_WITH_LAST_VALS:
197
	  gimp_get_data ("file_aa_save", &output_type);
198 199 200 201 202
	  break;

	default:
	  break;
	}
203
    }
204

Sven Neumann's avatar
Sven Neumann committed
205
  if (status == GIMP_PDB_SUCCESS)
206
    {
207
      if (save_aa (drawable_ID, param[3].data.d_string, output_type))
208
	{
209
	  gimp_set_data ("file_aa_save", &output_type, sizeof (output_type));
210 211 212
	}
      else
	{
213
	  status = GIMP_PDB_EXECUTION_ERROR;
214
	}
215
    }
216

217
  if (export == GIMP_EXPORT_EXPORT)
218
    gimp_image_delete (image_ID);  
219 220

  values[0].data.d_status = status;
221 222 223 224 225 226
}

/**
 * The actual save function. What it's all about.
 * The image type has to be GRAY.
 */
227
static gboolean
228 229 230
save_aa (gint32  drawable_ID,
         gchar  *filename,
         gint    output_type)
231
{
232 233 234
  aa_savedata  savedata;
  aa_context  *context;
  aa_format    format = *aa_formats[output_type];;
235

236 237
  format.width  = gimp_drawable_width (drawable_ID)  / 2;
  format.height = gimp_drawable_height (drawable_ID) / 2;
238

239
  /* Get a libaa context which will save its output to filename. */
240
  savedata.name   = filename;
241
  savedata.format = &format;
242

243
  context = aa_init (&save_d, &aa_defparams, &savedata);
244 245 246 247 248
  if (!context)
    return FALSE;

  g_assert (aa_imgwidth  (context) == gimp_drawable_width  (drawable_ID));
  g_assert (aa_imgheight (context) == gimp_drawable_height (drawable_ID));
249

250
  gimp2aa (drawable_ID, context);
251 252
  aa_flush (context);
  aa_close (context);
253

254
  return TRUE;
255 256
}

257
static void
258
gimp2aa (gint32      drawable_ID, 
259
	 aa_context *context)
260
{
261 262 263 264 265 266 267 268
  GimpDrawable    *drawable;
  GimpPixelRgn     pixel_rgn;
  aa_renderparams *renderparams;

  gint    width;
  gint    height;
  gint    x, y;
  gint    bpp;
269
  guchar *buffer;
270

271 272 273
  drawable = gimp_drawable_get (drawable_ID);

  width  = aa_imgwidth  (context);
274
  height = aa_imgheight (context);
275
  bpp    = drawable->bpp;
276

277 278 279
  gimp_pixel_rgn_init (&pixel_rgn, 
                       drawable, 0, 0, width, height, 
                       FALSE, FALSE);
280 281 282

  buffer = g_new (guchar, width * bpp);

283 284 285 286 287
  for (y = 0; y < height; y++) 
    {
      gimp_pixel_rgn_get_row (&pixel_rgn, buffer, 0, y, width);
      for (x = 0; x < width; x++) 
	{
288
          /* FIXME: add support for alpha channel */
289
	  aa_putpixel (context, x, y, buffer[x * bpp]);
290
	}
291
    }
292

293 294
  g_free (buffer);

295 296
  renderparams = aa_getrenderparams ();
  renderparams->dither = AA_FLOYD_S;
297

298
  aa_render (context, renderparams, 0, 0, 
299
	     aa_scrwidth (context), aa_scrheight (context));
300 301 302 303 304 305
}

/* 
 * User Interface dialog thingie.
 */

306
static gint 
307
type_dialog (gint selected) 
308 309 310 311 312
{
  GtkWidget *dlg;
  GtkWidget *toggle;
  GtkWidget *frame;
  GtkWidget *toggle_vbox;
313
  GSList    *group;
314

315
  /* Create the actual window. */
316
  dlg = gimp_dialog_new (_("Save as Text"), "aa",
317
			 gimp_standard_help_func, "filters/aa.html",
318 319 320
			 GTK_WIN_POS_MOUSE,
			 FALSE, TRUE, FALSE,

321
			 GTK_STOCK_CANCEL, type_dialog_cancel_callback,
322 323
			 NULL, NULL, NULL, FALSE, TRUE,

Sven Neumann's avatar
Sven Neumann committed
324 325 326
			 GTK_STOCK_OK, gtk_widget_destroy,
			 NULL, 1, NULL, TRUE, FALSE,

327
			 NULL);
Sven Neumann's avatar
Sven Neumann committed
328

329 330 331
  g_signal_connect (G_OBJECT (dlg), "destroy",
                    G_CALLBACK (gtk_main_quit),
                    NULL);
332 333

  /*  file save type  */
334 335 336 337 338 339 340 341
  frame = gtk_frame_new (_("Data Formatting"));
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  gtk_container_set_border_width (GTK_CONTAINER (frame), 6);
  gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dlg)->vbox), frame, TRUE, TRUE, 0);

  toggle_vbox = gtk_vbox_new (FALSE, 2);
  gtk_container_set_border_width (GTK_CONTAINER(toggle_vbox), 4);
  gtk_container_add (GTK_CONTAINER (frame), toggle_vbox);
342 343 344
  
  group = NULL;
  {
345
    aa_format **p = (aa_format **) aa_formats;
346 347
    gint current = 0;

348 349 350
    while (*p != NULL) 
      {
	toggle = gtk_radio_button_new_with_label (group, (*p)->formatname);
351
	group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (toggle));
352
	gtk_box_pack_start (GTK_BOX (toggle_vbox), toggle, FALSE, FALSE, 0);
353 354 355 356 357 358
	gtk_widget_show (toggle);

	g_signal_connect (G_OBJECT (toggle), "toggled",
                          G_CALLBACK (type_dialog_toggle_update),
                          (gpointer) (*p)->formatname);

359
	if (current == selected)
360
	  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), TRUE);
361 362 363 364 365
	
	p++;
	current++;
      }
  }
366

367 368
  gtk_widget_show (toggle_vbox);
  gtk_widget_show (frame);
369

370 371 372 373 374 375
  gtk_widget_show (dlg);

  gtk_main ();
  gdk_flush ();

  return selected_type;
376 377 378 379 380 381
}

/*
 * Callbacks for the dialog.
 */

382 383 384 385 386
static void 
type_dialog_cancel_callback (GtkWidget *widget, 
			     gpointer   data) 
{
  selected_type = -1;
387 388 389
  gtk_widget_destroy (GTK_WIDGET (data));
}

390 391 392 393
static void 
type_dialog_toggle_update (GtkWidget *widget, 
			   gpointer   data) 
{
394
  selected_type = get_type_from_string ((gchar *) data);
395
}