aa.c 10.2 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 15
#include <aalib.h>
#include <string.h>
#include <libgimp/gimp.h>
16
#include <libgimp/gimpui.h>
17 18 19 20
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>

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

23 24 25
/* 
 * Declare some local functions.
 */
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
static void query      (void);
static void run        (char       *name, 
			int         nparams, 
			GParam     *param, 
		        int        *nreturn_vals, 
			GParam    **return_vals);
static void init_gtk   (void);
static gint aa_savable (gint32      drawable_ID);
static gint save_aa    (int         output_type, 
			char       *filename, 
			gint32      image,
			gint32      drawable);
static gint gimp2aa    (gint32      image, 
			gint32      drawable_ID, 
			aa_context *context);

static gint type_dialog                 (int        selected);
static void type_dialog_close_callback  (GtkWidget *widget,
					 gpointer   data);
static void type_dialog_ok_callback     (GtkWidget *widget,
					 gpointer   data);
static void type_dialog_toggle_update   (GtkWidget *widget, 
					 gpointer   data);
static void type_dialog_cancel_callback (GtkWidget *widget, 
					 gpointer data);
51 52 53 54 55 56 57

/* 
 * Some global variables.
 */

GPlugInInfo PLUG_IN_INFO =
{
58 59 60 61
  NULL,  /* init_proc */
  NULL,  /* quit_proc */
  query, /* query_proc */
  run,   /* run_proc */
62 63 64 65 66 67 68 69 70 71 72 73
};

/**
 * Type the user selected. (Global for easier UI coding.
 */
static int selected_type = 0;


MAIN()
/**
 * Called by the GIMP to figure out what this plugin does.
 */
74 75
static void 
query()
76
{
77 78 79 80 81 82 83 84 85 86 87
  static GParamDef save_args[] =
  {
    {PARAM_INT32,    "run_mode",     "Interactive, non-interactive"},
    {PARAM_IMAGE,    "image",        "Input image"},
    {PARAM_DRAWABLE, "drawable",     "Drawable to save"},
    {PARAM_STRING,   "filename",     "The name of the file to save the image in"},
    {PARAM_STRING,   "raw_filename", "The name entered"},
    {PARAM_STRING,   "file_type",    "File type to use"}
  };
  static int nsave_args = sizeof(save_args) / sizeof(save_args[0]);

Shirasaki Yasuhiro's avatar
Shirasaki Yasuhiro committed
88 89
  INIT_I18N();

90
  gimp_install_procedure("file_aa_save",
Shirasaki Yasuhiro's avatar
Shirasaki Yasuhiro committed
91 92
			 _("Saves files in various text formats"),
			 _("Saves files in various text formats"),
93 94 95 96 97 98 99 100 101 102
			 "Tim Newsome <nuisance@cmu.edu>",
			 "Tim Newsome <nuisance@cmu.edu>",
			 "1997",
			 "<Save>/AA",
			 "GRAY*",		/* support grayscales */
			 PROC_PLUG_IN,
			 nsave_args, 0,
			 save_args, NULL);
  
  gimp_register_save_handler("file_aa_save", "ansi,txt,text,html", "");
103 104 105 106 107 108 109
}

/**
 * Searches aa_formats defined by aalib to find the index of the type
 * specified by string.
 * -1 means it wasn't found.
 */
110 111
static int 
get_type_from_string (char *string)
112
{
113 114 115 116 117 118 119 120 121 122 123 124
  int type = 0;
  aa_format **p = aa_formats;
  
  while (*p && strcmp((*p)->formatname, string)) {
    p++;
    type++;
  }

  if (*p == NULL)
    return -1;
  
  return type;
125 126 127 128 129
}

/**
 * Called by the GIMP to run the actual plugin.
 */
130 131 132 133 134 135
static void 
run (char    *name, 
     int      nparams, 
     GParam  *param, 
     int     *nreturn_vals,
     GParam **return_vals)
136
{
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  static GParam values[2];
  GStatusType status = STATUS_SUCCESS;
  GRunModeType run_mode;
  int output_type = 0;
  static int last_type = 0;
  gint32 image_ID;
  gint32 drawable_ID;
  GimpExportReturnType export = EXPORT_CANCEL;
 

  /* Set us up to return a status. */
  *nreturn_vals = 1;
  *return_vals = values;
  values[0].type = PARAM_STATUS;
  values[0].data.d_status = STATUS_CALLING_ERROR;
  
153 154 155 156 157 158 159 160 161
  run_mode    = param[0].data.d_int32;
  image_ID    = param[1].data.d_int32;
  drawable_ID = param[2].data.d_int32;
  
  /*  eventually export the image */ 
  switch (run_mode)
    {
    case RUN_INTERACTIVE:
    case RUN_WITH_LAST_VALS:
Shirasaki Yasuhiro's avatar
Shirasaki Yasuhiro committed
162
      INIT_I18N_UI();
163 164 165 166 167 168 169 170 171 172
      init_gtk ();
      export = gimp_export_image (&image_ID, &drawable_ID, "AA", 
				  (CAN_HANDLE_GRAY | CAN_HANDLE_ALPHA));
      if (export == EXPORT_CANCEL)
	{
	  values[0].data.d_status = STATUS_EXECUTION_ERROR;
	  return;
	}
      break;
    default:
Shirasaki Yasuhiro's avatar
Shirasaki Yasuhiro committed
173
      INIT_I18N();
174 175 176 177
      break;
    }
  
  if (!aa_savable (drawable_ID)) 
178 179 180 181 182 183 184 185
    {
      values[0].data.d_status = STATUS_CALLING_ERROR;
      return;
    }
  
  switch (run_mode) 
    {
    case RUN_INTERACTIVE:
186
      gimp_get_data ("file_aa_save", &last_type);
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
      output_type = type_dialog (last_type);
      break;
      
    case RUN_NONINTERACTIVE:
      /*  Make sure all the arguments are there!  */
      if (nparams != 6)
	status = STATUS_CALLING_ERROR;
      else
	output_type = get_type_from_string (param[5].data.d_string);
      break;
      
    case RUN_WITH_LAST_VALS:
      gimp_get_data ("file_aa_save", &last_type);
      output_type = last_type;
      break;
      
    default:
      break;
    }
  
  if (output_type < 0) 
    {
      status = STATUS_CALLING_ERROR;
      return;
    }
  
213
  if (save_aa (output_type, param[3].data.d_string, image_ID, drawable_ID))
214 215 216 217 218 219 220 221 222 223
    {
      values[0].data.d_status = STATUS_EXECUTION_ERROR;
      last_type = output_type;
      gimp_set_data ("file_aa_save", &last_type, sizeof(last_type));
    }    
  else
    values[0].data.d_status = STATUS_SUCCESS;
  
  if (export == EXPORT_EXPORT)
    gimp_image_delete (image_ID);  
224 225 226 227 228 229
}

/**
 * The actual save function. What it's all about.
 * The image type has to be GRAY.
 */
230 231 232
static gint 
save_aa (int     output_type, 
	 char   *filename, 
233
	 gint32  image_ID,
234
	 gint32  drawable_ID)
235
{
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
  aa_savedata savedata = {NULL, NULL};
  aa_context *context  = NULL;
  GDrawable *drawable  = NULL;
  aa_format format;
  
  /*fprintf(stderr, "save %s\n", filename); */
  
  drawable = gimp_drawable_get (drawable_ID);
  memcpy (&format, aa_formats[output_type], sizeof (format));
  format.width = drawable->width / 2;
  format.height = drawable->height / 2;
  
  /*fprintf(stderr, "save_aa %i x %i\n", format.width, format.height); */
  
  /* Get a libaa context which will save its output to filename. */
  savedata.name = filename;
  savedata.format = &format;
  
  context = aa_init (&save_d, &aa_defparams, &savedata);
  if (context == NULL)
    return 1;
  
258
  gimp2aa (image_ID, drawable_ID, context);
259 260 261 262 263 264
  aa_flush (context);
  aa_close (context);
  
  /*fprintf(stderr, "Success!\n"); */
  
  return 0;
265 266
}

267 268 269 270
static gint 
gimp2aa (gint32      image, 
	 gint32      drawable_ID, 
	 aa_context *context)
271
{
272 273 274 275 276 277 278
  int width, height, x, y;
  guchar *buffer;
  GDrawable *drawable = NULL;
  GPixelRgn pixel_rgn;
  aa_renderparams *renderparams = NULL;
  int bpp;
  
279 280
  width = aa_imgwidth (context);
  height = aa_imgheight (context);
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
  /*fprintf(stderr, "gimp2aa %i x %i\n", width, height); */
  
  drawable = gimp_drawable_get(drawable_ID);
  
  bpp = drawable->bpp;
  buffer = g_new(guchar, width * bpp);
  if (buffer == NULL)
    return 1;
  
  gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0, drawable->width,
		       drawable->height, FALSE, FALSE);

  for (y = 0; y < height; y++) 
    {
      gimp_pixel_rgn_get_row (&pixel_rgn, buffer, 0, y, width);
      for (x = 0; x < width; x++) 
	{
	  /* Just copy one byte. If it's indexed that's all we need. Otherwise
	   * it'll be the most significant one. */
	  aa_putpixel(context, x, y, buffer[x * bpp]);
301
	}
302 303 304 305 306 307 308 309
    }
  
  renderparams = aa_getrenderparams ();
  renderparams->dither = AA_FLOYD_S;
  aa_render (context, renderparams, 0, 0, 
	    aa_scrwidth (context), aa_scrheight (context));

  return 0;
310 311
}

312 313
static gint 
aa_savable (gint32 drawable_ID)
314
{
315 316 317 318 319 320 321 322
  GDrawableType drawable_type;
  
  drawable_type = gimp_drawable_type (drawable_ID);
  
  if (drawable_type != GRAY_IMAGE && drawable_type != GRAYA_IMAGE)
    return 0;
  
  return 1;
323 324 325 326 327 328
}

/* 
 * User Interface dialog thingie.
 */

329 330 331 332 333 334 335 336 337 338 339 340 341
static void 
init_gtk ()
{
  gchar **argv;
  gint argc;

  argc = 1;
  argv = g_new (gchar *, 1);
  argv[0] = g_strdup ("save");
  
  gtk_init (&argc, &argv);
  gtk_rc_parse (gimp_gtkrc ());
}
342

343 344 345 346 347 348 349 350 351 352
static gint 
type_dialog (int selected) 
{
  GtkWidget *dlg;
  GtkWidget *toggle;
  GtkWidget *frame;
  GtkWidget *toggle_vbox;
  GSList *group;
  
  /* Create the actual window. */
353 354 355 356 357 358 359 360 361 362 363
  dlg = gimp_dialog_new (_("Save as Text"), "aa",
			 gimp_plugin_help_func, "filters/aa.html",
			 GTK_WIN_POS_MOUSE,
			 FALSE, TRUE, FALSE,

			 _("OK"), type_dialog_ok_callback,
			 NULL, NULL, NULL, TRUE, FALSE,
			 _("Cancel"), type_dialog_cancel_callback,
			 NULL, NULL, NULL, FALSE, TRUE,

			 NULL);
Sven Neumann's avatar
Sven Neumann committed
364

365 366 367
  gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
		      GTK_SIGNAL_FUNC (type_dialog_close_callback),
		      NULL);
368 369

  /*  file save type  */
Shirasaki Yasuhiro's avatar
Shirasaki Yasuhiro committed
370
  frame = gtk_frame_new ( _("Data Formatting"));
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
  gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_ETCHED_IN);
  gtk_container_border_width(GTK_CONTAINER(frame), 10);
  gtk_box_pack_start (GTK_BOX(GTK_DIALOG(dlg)->vbox), frame, FALSE, TRUE, 0);
  toggle_vbox = gtk_vbox_new (FALSE, 5);
  gtk_container_border_width (GTK_CONTAINER(toggle_vbox), 5);
  gtk_container_add (GTK_CONTAINER(frame), toggle_vbox);
  
  group = NULL;
  {
    aa_format **p = aa_formats;
    int current = 0;
    
    while (*p != NULL) 
      {
	toggle = gtk_radio_button_new_with_label (group, (*p)->formatname);
	group = gtk_radio_button_group (GTK_RADIO_BUTTON(toggle));
	gtk_box_pack_start (GTK_BOX (toggle_vbox), toggle, FALSE, FALSE, 0);
	gtk_signal_connect (GTK_OBJECT(toggle), "toggled",
			    (GtkSignalFunc) type_dialog_toggle_update,
			    (*p)->formatname);
	if (current == selected)
	  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(toggle), 1);
	else
	  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(toggle), 0);
	
	gtk_widget_show (toggle);
	p++;
	current++;
      }
  }
  
  gtk_widget_show (toggle_vbox);
  gtk_widget_show (frame);
  
  gtk_widget_show (dlg);

  gtk_main ();
  gdk_flush ();

  return selected_type;
411 412 413 414 415 416
}

/*
 * Callbacks for the dialog.
 */

417 418 419 420 421
static void 
type_dialog_close_callback (GtkWidget *widget, 
			    gpointer   data) 
{
  gtk_main_quit ();
422 423
}

424 425 426 427
static void 
type_dialog_ok_callback (GtkWidget *widget, 
			 gpointer   data) 
{
428 429 430
  gtk_widget_destroy (GTK_WIDGET (data));
}

431 432 433 434 435
static void 
type_dialog_cancel_callback (GtkWidget *widget, 
			     gpointer   data) 
{
  selected_type = -1;
436 437 438
  gtk_widget_destroy (GTK_WIDGET (data));
}

439 440 441 442 443
static void 
type_dialog_toggle_update (GtkWidget *widget, 
			   gpointer   data) 
{
  selected_type = get_type_from_string ((char *)data);
444
}