aa.c 10.5 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

45
/*
46 47 48
 * Some global variables.
 */

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

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


63 64
MAIN ()

65
static void
66
query (void)
67
{
Sven Neumann's avatar
Sven Neumann committed
68
  static GimpParamDef save_args[] =
69
  {
Sven Neumann's avatar
Sven Neumann committed
70 71 72 73 74 75
    {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"}
76 77
  };

78
  gimp_install_procedure ("file_aa_save",
79 80
                          "Saves grayscale image in various text formats",
                          "This plug-in uses aalib to save grayscale image "
81
                          "as ascii art into a variety of text formats",
82 83 84 85 86 87 88 89
                          "Tim Newsome <nuisance@cmu.edu>",
                          "Tim Newsome <nuisance@cmu.edu>",
                          "1997",
                          "<Save>/AA",
                          "RGB*, GRAY*",
                          GIMP_PLUGIN,
                          G_N_ELEMENTS (save_args), 0,
                          save_args, NULL);
90

91
  gimp_register_save_handler ("file_aa_save",
92 93
                              "ansi,txt,text",
                              "");
94 95 96 97 98 99 100
}

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

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

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

116
  return type;
117 118
}

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

134 135
  INIT_I18N ();

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

Sven Neumann's avatar
Sven Neumann committed
165
  if (! (gimp_drawable_is_rgb (drawable_ID) ||
166
         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
      switch (run_mode)
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
        {
        case GIMP_RUN_INTERACTIVE:
          gimp_get_data ("file_aa_save", &output_type);
          output_type = type_dialog (output_type);
          if (output_type < 0)
            status = GIMP_PDB_CANCEL;
          break;

        case GIMP_RUN_NONINTERACTIVE:
          /*  Make sure all the arguments are there!  */
          if (nparams != 6)
            {
              status = GIMP_PDB_CALLING_ERROR;
            }
          else
            {
              output_type = get_type_from_string (param[5].data.d_string);
              if (output_type < 0)
                status = GIMP_PDB_CALLING_ERROR;
            }
          break;

        case GIMP_RUN_WITH_LAST_VALS:
          gimp_get_data ("file_aa_save", &output_type);
          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 210
        {
          gimp_set_data ("file_aa_save", &output_type, sizeof (output_type));
        }
211
      else
212 213 214
        {
          status = GIMP_PDB_EXECUTION_ERROR;
        }
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
  aa_savedata  savedata;
  aa_context  *context;
234
  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
  if (!context)
    return FALSE;

  gimp2aa (drawable_ID, context);
248 249
  aa_flush (context);
  aa_close (context);
250

251
  return TRUE;
252 253
}

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

  gint    width;
  gint    height;
  gint    x, y;
  gint    bpp;
266
  guchar *buffer;
Sven Neumann's avatar
Sven Neumann committed
267
  guchar *p;
268

269 270 271
  drawable = gimp_drawable_get (drawable_ID);

  width  = aa_imgwidth  (context);
272
  height = aa_imgheight (context);
273
  bpp    = drawable->bpp;
274

275 276 277 278
  gimp_tile_cache_ntiles ((width / gimp_tile_width ()) + 1);

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

  buffer = g_new (guchar, width * bpp);

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

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

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

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

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

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

318 319
  g_free (buffer);

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

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

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

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

340
  /* Create the actual window. */
341
  dlg = gimp_dialog_new (_("Save as Text"), "aa",
342
                         NULL, 0,
343
                         gimp_standard_help_func, "file-aa-save",
344

345 346
                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                         GTK_STOCK_OK,     GTK_RESPONSE_OK,
Sven Neumann's avatar
Sven Neumann committed
347

348
                         NULL);
Sven Neumann's avatar
Sven Neumann committed
349

350
  /*  file save type  */
351 352 353 354 355 356 357
  frame = gtk_frame_new (_("Data Formatting"));
  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);
358

359 360
  group = NULL;
  {
361
    aa_format **p = (aa_format **) aa_formats;
362 363
    gint current = 0;

364
    while (*p != NULL)
365
      {
366 367 368 369
        toggle = gtk_radio_button_new_with_label (group, (*p)->formatname);
        group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (toggle));
        gtk_box_pack_start (GTK_BOX (toggle_vbox), toggle, FALSE, FALSE, 0);
        gtk_widget_show (toggle);
370

371
        g_signal_connect (toggle, "toggled",
372 373 374
                          G_CALLBACK (type_dialog_toggle_update),
                          (gpointer) (*p)->formatname);

375 376
        if (current == selected)
          gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), TRUE);
377

378 379
        p++;
        current++;
380 381
      }
  }
382

383 384
  gtk_widget_show (toggle_vbox);
  gtk_widget_show (frame);
385

386 387
  gtk_widget_show (dlg);

388
  if (gimp_dialog_run (GIMP_DIALOG (dlg)) != GTK_RESPONSE_OK)
389 390 391
    selected_type = -1;

  gtk_widget_destroy (dlg);
392 393

  return selected_type;
394 395 396 397 398 399
}

/*
 * Callbacks for the dialog.
 */

400 401
static void
type_dialog_toggle_update (GtkWidget *widget,
402
                           gpointer   data)
403
{
404
  selected_type = get_type_from_string ((const gchar *) data);
405
}