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
#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
static void     query      (void);
30 31 32 33
static void     run        (const gchar      *name,
                            gint              nparams,
                            const GimpParam  *param,
                            gint             *nreturn_vals,
34 35
                            GimpParam       **return_vals);
static gboolean save_aa    (gint32            drawable_ID,
36
                            gchar            *filename,
37
                            gint              output_type);
38
static void     gimp2aa    (gint32            drawable_ID,
39
                            aa_context       *context);
40 41

static gint     type_dialog                 (gint       selected);
42
static void     type_dialog_toggle_update   (GtkWidget *widget,
43
                                             gpointer   data);
44
static void     type_dialog_cancel_callback (GtkWidget *widget,
45
                                             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",
Sven Neumann's avatar
Sven Neumann committed
88
			  "RGB*, GRAY*",
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
static gint
104
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 122 123 124
static void
run (const gchar      *name,
     gint              nparams,
     const GimpParam  *param,
125 126
     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
  INIT_I18N ();

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

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

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

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

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

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

	default:
	  break;
	}
205
    }
206

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

219
  if (export == GIMP_EXPORT_EXPORT)
220
    gimp_image_delete (image_ID);
221 222

  values[0].data.d_status = status;
223 224 225 226 227 228
}

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

238 239
  format.width  = gimp_drawable_width (drawable_ID)  / 2;
  format.height = gimp_drawable_height (drawable_ID) / 2;
240

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

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

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

253
  return TRUE;
254 255
}

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

  gint    width;
  gint    height;
  gint    x, y;
  gint    bpp;
268
  guchar *buffer;
Sven Neumann's avatar
Sven Neumann committed
269
  guchar *p;
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 280
  gimp_tile_cache_ntiles ((width / gimp_tile_width ()) + 1);

  gimp_pixel_rgn_init (&pixel_rgn,
                       drawable, 0, 0, width, height,
281
                       FALSE, FALSE);
282 283 284

  buffer = g_new (guchar, width * bpp);

285
  for (y = 0; y < height; y++)
286 287
    {
      gimp_pixel_rgn_get_row (&pixel_rgn, buffer, 0, y, width);
Sven Neumann's avatar
Sven Neumann committed
288 289 290 291

      switch (bpp)
        {
        case 1:  /* GRAY */
292
          for (x = 0, p = buffer; x < width; x++, p++)
Sven Neumann's avatar
Sven Neumann committed
293 294 295 296
            aa_putpixel (context, x, y, *p);
          break;

        case 2:  /* GRAYA, blend over black */
297
          for (x = 0, p = buffer; x < width; x++, p += 2)
Sven Neumann's avatar
Sven Neumann committed
298 299
            aa_putpixel (context, x, y, (p[0] * (p[1] + 1)) >> 8);
          break;
300

Sven Neumann's avatar
Sven Neumann committed
301
        case 3:  /* RGB */
302
          for (x = 0, p = buffer; x < width; x++, p += 3)
Sven Neumann's avatar
Sven Neumann committed
303 304
            aa_putpixel (context, x, y, INTENSITY (p[0], p[1], p[2]));
          break;
305

Sven Neumann's avatar
Sven Neumann committed
306
        case 4:  /* RGBA, blend over black */
307
          for (x = 0, p = buffer; x < width; x++, p += 4)
Sven Neumann's avatar
Sven Neumann committed
308 309 310 311 312 313 314 315 316
            aa_putpixel (context, x, y,
                         ((guchar) INTENSITY (p[0], p[1], p[2]) * (p[3] + 1))
                         >> 8);
          break;

        default:
          g_assert_not_reached ();
          break;
        }
317
    }
318

319 320
  g_free (buffer);

321 322
  renderparams = aa_getrenderparams ();
  renderparams->dither = AA_FLOYD_S;
323

324
  aa_render (context, renderparams, 0, 0,
325
	     aa_scrwidth (context), aa_scrheight (context));
326 327
}

328
/*
329 330 331
 * User Interface dialog thingie.
 */

332 333
static gint
type_dialog (gint selected)
334 335 336 337 338
{
  GtkWidget *dlg;
  GtkWidget *toggle;
  GtkWidget *frame;
  GtkWidget *toggle_vbox;
339
  GSList    *group;
340

341
  /* Create the actual window. */
342
  dlg = gimp_dialog_new (_("Save as Text"), "aa",
343
			 gimp_standard_help_func, "filters/aa.html",
344 345 346
			 GTK_WIN_POS_MOUSE,
			 FALSE, TRUE, FALSE,

347
			 GTK_STOCK_CANCEL, type_dialog_cancel_callback,
348 349
			 NULL, NULL, NULL, FALSE, TRUE,

Sven Neumann's avatar
Sven Neumann committed
350 351 352
			 GTK_STOCK_OK, gtk_widget_destroy,
			 NULL, 1, NULL, TRUE, FALSE,

353
			 NULL);
Sven Neumann's avatar
Sven Neumann committed
354

355
  g_signal_connect (dlg, "destroy",
356 357
                    G_CALLBACK (gtk_main_quit),
                    NULL);
358 359

  /*  file save type  */
360 361 362 363 364 365 366 367
  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);
368

369 370
  group = NULL;
  {
371
    aa_format **p = (aa_format **) aa_formats;
372 373
    gint current = 0;

374
    while (*p != NULL)
375 376
      {
	toggle = gtk_radio_button_new_with_label (group, (*p)->formatname);
377
	group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (toggle));
378
	gtk_box_pack_start (GTK_BOX (toggle_vbox), toggle, FALSE, FALSE, 0);
379 380
	gtk_widget_show (toggle);

381
	g_signal_connect (toggle, "toggled",
382 383 384
                          G_CALLBACK (type_dialog_toggle_update),
                          (gpointer) (*p)->formatname);

385
	if (current == selected)
386
	  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), TRUE);
387

388 389 390 391
	p++;
	current++;
      }
  }
392

393 394
  gtk_widget_show (toggle_vbox);
  gtk_widget_show (frame);
395

396 397 398 399 400 401
  gtk_widget_show (dlg);

  gtk_main ();
  gdk_flush ();

  return selected_type;
402 403 404 405 406 407
}

/*
 * Callbacks for the dialog.
 */

408 409 410
static void
type_dialog_cancel_callback (GtkWidget *widget,
			     gpointer   data)
411 412
{
  selected_type = -1;
413 414 415
  gtk_widget_destroy (GTK_WIDGET (data));
}

416 417 418
static void
type_dialog_toggle_update (GtkWidget *widget,
			   gpointer   data)
419
{
420
  selected_type = get_type_from_string ((const gchar *) data);
421
}