aa.c 11.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>
 */

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

Shirasaki Yasuhiro's avatar
Shirasaki Yasuhiro committed
29 30
#include "config.h"

31
#include <string.h>
32 33 34 35
#include <aalib.h>

#include <gtk/gtk.h>

36
#include <libgimp/gimp.h>
37
#include <libgimp/gimpui.h>
38

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

41
/*
42 43
 * Declare some local functions.
 */
44
static void     query      (void);
45 46 47 48
static void     run        (const gchar      *name,
                            gint              nparams,
                            const GimpParam  *param,
                            gint             *nreturn_vals,
49 50
                            GimpParam       **return_vals);
static gboolean save_aa    (gint32            drawable_ID,
51
                            gchar            *filename,
52
                            gint              output_type);
53
static void     gimp2aa    (gint32            drawable_ID,
54
                            aa_context       *context);
55 56

static gint     type_dialog                 (gint       selected);
57
static void     type_dialog_toggle_update   (GtkWidget *widget,
58
                                             gpointer   data);
59

60
/*
61 62 63
 * Some global variables.
 */

Sven Neumann's avatar
Sven Neumann committed
64
GimpPlugInInfo PLUG_IN_INFO =
65
{
66 67
  NULL,  /* init_proc  */
  NULL,  /* quit_proc  */
68
  query, /* query_proc */
69
  run,   /* run_proc   */
70 71 72
};

/**
73
 * Type the user selected. (Global for easier UI coding.)
74
 */
75
static gint selected_type = 0;
76 77


78 79
MAIN ()

80
static void
81
query (void)
82
{
Sven Neumann's avatar
Sven Neumann committed
83
  static GimpParamDef save_args[] =
84
  {
Sven Neumann's avatar
Sven Neumann committed
85 86 87 88 89 90
    {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"}
91 92
  };

93
  gimp_install_procedure ("file_aa_save",
94 95
                          "Saves grayscale image in various text formats",
                          "This plug-in uses aalib to save grayscale image "
96
                          "as ascii art into a variety of text formats",
97 98 99 100 101 102 103 104
                          "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);
105

106
  gimp_register_save_handler ("file_aa_save",
107 108
                              "ansi,txt,text",
                              "");
109 110 111 112 113 114 115
}

/**
 * Searches aa_formats defined by aalib to find the index of the type
 * specified by string.
 * -1 means it wasn't found.
 */
116
static gint
117
get_type_from_string (const gchar *string)
118
{
119
  gint type = 0;
120
  aa_format **p = (aa_format **) aa_formats;
121

122 123 124 125 126
  while (*p && strcmp ((*p)->formatname, string))
    {
      p++;
      type++;
    }
127 128 129

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

131
  return type;
132 133
}

134 135 136 137
static void
run (const gchar      *name,
     gint              nparams,
     const GimpParam  *param,
138 139
     gint             *nreturn_vals,
     GimpParam       **return_vals)
140
{
141 142 143 144 145 146 147
  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;
148

149 150
  INIT_I18N ();

151 152
  /* Set us up to return a status. */
  *nreturn_vals = 1;
153
  *return_vals  = values;
Sven Neumann's avatar
Sven Neumann committed
154 155
  values[0].type          = GIMP_PDB_STATUS;
  values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
156 157 158
  run_mode    = param[0].data.d_int32;
  image_ID    = param[1].data.d_int32;
  drawable_ID = param[2].data.d_int32;
159

160
  /*  eventually export the image */
161 162
  switch (run_mode)
    {
Sven Neumann's avatar
Sven Neumann committed
163 164
    case GIMP_RUN_INTERACTIVE:
    case GIMP_RUN_WITH_LAST_VALS:
165
      gimp_ui_init ("aa", FALSE);
166
      export = gimp_export_image (&image_ID, &drawable_ID, "AA",
167
                                  (GIMP_EXPORT_CAN_HANDLE_RGB  |
Sven Neumann's avatar
Sven Neumann committed
168
                                   GIMP_EXPORT_CAN_HANDLE_GRAY |
169
                                   GIMP_EXPORT_CAN_HANDLE_ALPHA ));
170
      if (export == GIMP_EXPORT_CANCEL)
171 172 173 174
        {
          values[0].data.d_status = GIMP_PDB_CANCEL;
          return;
        }
175 176 177 178
      break;
    default:
      break;
    }
179

Sven Neumann's avatar
Sven Neumann committed
180
  if (! (gimp_drawable_is_rgb (drawable_ID) ||
181
         gimp_drawable_is_gray (drawable_ID)))
182
    {
Sven Neumann's avatar
Sven Neumann committed
183
      status = GIMP_PDB_CALLING_ERROR;
184
    }
185

Sven Neumann's avatar
Sven Neumann committed
186
  if (status == GIMP_PDB_SUCCESS)
187
    {
188
      switch (run_mode)
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
        {
        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;
        }
218
    }
219

Sven Neumann's avatar
Sven Neumann committed
220
  if (status == GIMP_PDB_SUCCESS)
221
    {
222
      if (save_aa (drawable_ID, param[3].data.d_string, output_type))
223 224 225
        {
          gimp_set_data ("file_aa_save", &output_type, sizeof (output_type));
        }
226
      else
227 228 229
        {
          status = GIMP_PDB_EXECUTION_ERROR;
        }
230
    }
231

232
  if (export == GIMP_EXPORT_EXPORT)
233
    gimp_image_delete (image_ID);
234 235

  values[0].data.d_status = status;
236 237 238 239 240 241
}

/**
 * The actual save function. What it's all about.
 * The image type has to be GRAY.
 */
242
static gboolean
243 244 245
save_aa (gint32  drawable_ID,
         gchar  *filename,
         gint    output_type)
246
{
247 248
  aa_savedata  savedata;
  aa_context  *context;
249
  aa_format    format = *aa_formats[output_type];
250

251 252
  format.width  = gimp_drawable_width (drawable_ID)  / 2;
  format.height = gimp_drawable_height (drawable_ID) / 2;
253

254
  /* Get a libaa context which will save its output to filename. */
255
  savedata.name   = filename;
256
  savedata.format = &format;
257

258
  context = aa_init (&save_d, &aa_defparams, &savedata);
259 260 261 262
  if (!context)
    return FALSE;

  gimp2aa (drawable_ID, context);
263 264
  aa_flush (context);
  aa_close (context);
265

266
  return TRUE;
267 268
}

269
static void
270
gimp2aa (gint32      drawable_ID,
271
         aa_context *context)
272
{
273 274 275 276 277 278 279 280
  GimpDrawable    *drawable;
  GimpPixelRgn     pixel_rgn;
  aa_renderparams *renderparams;

  gint    width;
  gint    height;
  gint    x, y;
  gint    bpp;
281
  guchar *buffer;
Sven Neumann's avatar
Sven Neumann committed
282
  guchar *p;
283

284 285 286
  drawable = gimp_drawable_get (drawable_ID);

  width  = aa_imgwidth  (context);
287
  height = aa_imgheight (context);
288
  bpp    = drawable->bpp;
289

290 291 292 293
  gimp_tile_cache_ntiles ((width / gimp_tile_width ()) + 1);

  gimp_pixel_rgn_init (&pixel_rgn,
                       drawable, 0, 0, width, height,
294
                       FALSE, FALSE);
295 296 297

  buffer = g_new (guchar, width * bpp);

298
  for (y = 0; y < height; y++)
299 300
    {
      gimp_pixel_rgn_get_row (&pixel_rgn, buffer, 0, y, width);
Sven Neumann's avatar
Sven Neumann committed
301 302 303 304

      switch (bpp)
        {
        case 1:  /* GRAY */
305
          for (x = 0, p = buffer; x < width; x++, p++)
Sven Neumann's avatar
Sven Neumann committed
306 307 308 309
            aa_putpixel (context, x, y, *p);
          break;

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

Sven Neumann's avatar
Sven Neumann committed
314
        case 3:  /* RGB */
315
          for (x = 0, p = buffer; x < width; x++, p += 3)
316 317
            aa_putpixel (context, x, y,
                         GIMP_RGB_INTENSITY (p[0], p[1], p[2]) + 0.5);
Sven Neumann's avatar
Sven Neumann committed
318
          break;
319

Sven Neumann's avatar
Sven Neumann committed
320
        case 4:  /* RGBA, blend over black */
321
          for (x = 0, p = buffer; x < width; x++, p += 4)
Sven Neumann's avatar
Sven Neumann committed
322
            aa_putpixel (context, x, y,
323 324
                         ((guchar) (GIMP_RGB_INTENSITY (p[0], p[1], p[2]) + 0.5)
                          * (p[3] + 1)) >> 8);
Sven Neumann's avatar
Sven Neumann committed
325 326 327 328 329 330
          break;

        default:
          g_assert_not_reached ();
          break;
        }
331
    }
332

333 334
  g_free (buffer);

335 336
  renderparams = aa_getrenderparams ();
  renderparams->dither = AA_FLOYD_S;
337

338
  aa_render (context, renderparams, 0, 0,
339
             aa_scrwidth (context), aa_scrheight (context));
340 341
}

342 343
static gint
type_dialog (gint selected)
344 345 346 347 348
{
  GtkWidget *dlg;
  GtkWidget *toggle;
  GtkWidget *frame;
  GtkWidget *toggle_vbox;
349
  GSList    *group;
350

351
  /* Create the actual window. */
352
  dlg = gimp_dialog_new (_("Save as Text"), "aa",
353
                         NULL, 0,
354
                         gimp_standard_help_func, "file-aa-save",
355

356 357
                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                         GTK_STOCK_OK,     GTK_RESPONSE_OK,
Sven Neumann's avatar
Sven Neumann committed
358

359
                         NULL);
Sven Neumann's avatar
Sven Neumann committed
360

361
  /*  file save type  */
362 363
  frame = gimp_frame_new (_("Data Formatting"));
  gtk_container_set_border_width (GTK_CONTAINER (frame), 12);
364 365 366 367 368
  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);
369

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

375
    while (*p != NULL)
376
      {
377 378 379 380
        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);
381

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

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

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

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

397 398
  gtk_widget_show (dlg);

399
  if (gimp_dialog_run (GIMP_DIALOG (dlg)) != GTK_RESPONSE_OK)
400 401 402
    selected_type = -1;

  gtk_widget_destroy (dlg);
403 404

  return selected_type;
405 406
}

407 408
static void
type_dialog_toggle_update (GtkWidget *widget,
409
                           gpointer   data)
410
{
411
  selected_type = get_type_from_string ((const gchar *) data);
412
}