aa.c 10.9 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 45 46 47 48 49 50 51 52 53 54 55 56 57
static void     query       (void);
static void     run         (const gchar      *name,
                             gint              nparams,
                             const 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     aa_dialog   (gint              selected);

58

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

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


72 73
MAIN ()

74
static void
75
query (void)
76
{
Sven Neumann's avatar
Sven Neumann committed
77
  static GimpParamDef save_args[] =
78
  {
Sven Neumann's avatar
Sven Neumann committed
79 80 81 82 83 84
    {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"}
85 86
  };

87
  gimp_install_procedure ("file_aa_save",
88 89
                          "Saves grayscale image in various text formats",
                          "This plug-in uses aalib to save grayscale image "
90
                          "as ascii art into a variety of text formats",
91 92 93
                          "Tim Newsome <nuisance@cmu.edu>",
                          "Tim Newsome <nuisance@cmu.edu>",
                          "1997",
94
                          N_("ASCII art"),
95 96 97 98
                          "RGB*, GRAY*",
                          GIMP_PLUGIN,
                          G_N_ELEMENTS (save_args), 0,
                          save_args, NULL);
99

100
  gimp_register_file_handler_mime ("file_aa_save", "text/plain");
101
  gimp_register_save_handler ("file_aa_save", "txt,ansi,text", "");
102 103 104 105 106 107 108
}

/**
 * Searches aa_formats defined by aalib to find the index of the type
 * specified by string.
 * -1 means it wasn't found.
 */
109
static gint
110
get_type_from_string (const gchar *string)
111
{
112
  gint type = 0;
113
  aa_format **p = (aa_format **) aa_formats;
114

115 116 117 118 119
  while (*p && strcmp ((*p)->formatname, string))
    {
      p++;
      type++;
    }
120 121 122

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

124
  return type;
125 126
}

127 128 129 130
static void
run (const gchar      *name,
     gint              nparams,
     const GimpParam  *param,
131 132
     gint             *nreturn_vals,
     GimpParam       **return_vals)
133
{
134 135 136 137 138 139 140
  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;
141

142 143
  INIT_I18N ();

144 145
  /* Set us up to return a status. */
  *nreturn_vals = 1;
146
  *return_vals  = values;
Sven Neumann's avatar
Sven Neumann committed
147 148
  values[0].type          = GIMP_PDB_STATUS;
  values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
149 150 151
  run_mode    = param[0].data.d_int32;
  image_ID    = param[1].data.d_int32;
  drawable_ID = param[2].data.d_int32;
152

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

Sven Neumann's avatar
Sven Neumann committed
173
  if (! (gimp_drawable_is_rgb (drawable_ID) ||
174
         gimp_drawable_is_gray (drawable_ID)))
175
    {
Sven Neumann's avatar
Sven Neumann committed
176
      status = GIMP_PDB_CALLING_ERROR;
177
    }
178

Sven Neumann's avatar
Sven Neumann committed
179
  if (status == GIMP_PDB_SUCCESS)
180
    {
181
      switch (run_mode)
182 183 184
        {
        case GIMP_RUN_INTERACTIVE:
          gimp_get_data ("file_aa_save", &output_type);
185
          output_type = aa_dialog (output_type);
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
          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;
        }
211
    }
212

Sven Neumann's avatar
Sven Neumann committed
213
  if (status == GIMP_PDB_SUCCESS)
214
    {
215
      if (save_aa (drawable_ID, param[3].data.d_string, output_type))
216 217 218
        {
          gimp_set_data ("file_aa_save", &output_type, sizeof (output_type));
        }
219
      else
220 221 222
        {
          status = GIMP_PDB_EXECUTION_ERROR;
        }
223
    }
224

225
  if (export == GIMP_EXPORT_EXPORT)
226
    gimp_image_delete (image_ID);
227 228

  values[0].data.d_status = status;
229 230 231 232 233 234
}

/**
 * The actual save function. What it's all about.
 * The image type has to be GRAY.
 */
235
static gboolean
236 237 238
save_aa (gint32  drawable_ID,
         gchar  *filename,
         gint    output_type)
239
{
240 241
  aa_savedata  savedata;
  aa_context  *context;
242
  aa_format    format = *aa_formats[output_type];
243

244 245
  format.width  = gimp_drawable_width (drawable_ID)  / 2;
  format.height = gimp_drawable_height (drawable_ID) / 2;
246

247
  /* Get a libaa context which will save its output to filename. */
248
  savedata.name   = filename;
249
  savedata.format = &format;
250

251
  context = aa_init (&save_d, &aa_defparams, &savedata);
252 253 254 255
  if (!context)
    return FALSE;

  gimp2aa (drawable_ID, context);
256 257
  aa_flush (context);
  aa_close (context);
258

259
  return TRUE;
260 261
}

262
static void
263
gimp2aa (gint32      drawable_ID,
264
         aa_context *context)
265
{
266 267 268 269 270 271 272 273
  GimpDrawable    *drawable;
  GimpPixelRgn     pixel_rgn;
  aa_renderparams *renderparams;

  gint    width;
  gint    height;
  gint    x, y;
  gint    bpp;
274
  guchar *buffer;
Sven Neumann's avatar
Sven Neumann committed
275
  guchar *p;
276

277 278 279
  drawable = gimp_drawable_get (drawable_ID);

  width  = aa_imgwidth  (context);
280
  height = aa_imgheight (context);
281
  bpp    = drawable->bpp;
282

283 284 285 286
  gimp_tile_cache_ntiles ((width / gimp_tile_width ()) + 1);

  gimp_pixel_rgn_init (&pixel_rgn,
                       drawable, 0, 0, width, height,
287
                       FALSE, FALSE);
288 289 290

  buffer = g_new (guchar, width * bpp);

291
  for (y = 0; y < height; y++)
292 293
    {
      gimp_pixel_rgn_get_row (&pixel_rgn, buffer, 0, y, width);
Sven Neumann's avatar
Sven Neumann committed
294 295 296 297

      switch (bpp)
        {
        case 1:  /* GRAY */
298
          for (x = 0, p = buffer; x < width; x++, p++)
Sven Neumann's avatar
Sven Neumann committed
299 300 301 302
            aa_putpixel (context, x, y, *p);
          break;

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

Sven Neumann's avatar
Sven Neumann committed
307
        case 3:  /* RGB */
308
          for (x = 0, p = buffer; x < width; x++, p += 3)
309 310
            aa_putpixel (context, x, y,
                         GIMP_RGB_INTENSITY (p[0], p[1], p[2]) + 0.5);
Sven Neumann's avatar
Sven Neumann committed
311
          break;
312

Sven Neumann's avatar
Sven Neumann committed
313
        case 4:  /* RGBA, blend over black */
314
          for (x = 0, p = buffer; x < width; x++, p += 4)
Sven Neumann's avatar
Sven Neumann committed
315
            aa_putpixel (context, x, y,
316 317
                         ((guchar) (GIMP_RGB_INTENSITY (p[0], p[1], p[2]) + 0.5)
                          * (p[3] + 1)) >> 8);
Sven Neumann's avatar
Sven Neumann committed
318 319 320 321 322 323
          break;

        default:
          g_assert_not_reached ();
          break;
        }
324
    }
325

326 327
  g_free (buffer);

328 329
  renderparams = aa_getrenderparams ();
  renderparams->dither = AA_FLOYD_S;
330

331
  aa_render (context, renderparams, 0, 0,
332
             aa_scrwidth (context), aa_scrheight (context));
333 334
}

335
static gint
336
aa_dialog (gint selected)
337
{
338 339 340 341 342
  GtkWidget *dialog;
  GtkWidget *hbox;
  GtkWidget *label;
  GtkWidget *combo;
  gint       i;
343

344
  /* Create the actual window. */
345
  dialog = gimp_dialog_new (_("Save as Text"), "aa",
346
                         NULL, 0,
347
                         gimp_standard_help_func, "file-aa-save",
348

349 350
                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                         GTK_STOCK_OK,     GTK_RESPONSE_OK,
Sven Neumann's avatar
Sven Neumann committed
351

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

354 355 356 357 358
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                              GTK_RESPONSE_OK,
                                              GTK_RESPONSE_CANCEL,
                                              -1);

359 360 361 362 363
  hbox = gtk_hbox_new (FALSE, 6);
  gtk_container_set_border_width (GTK_CONTAINER (hbox), 12);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
                      hbox, FALSE, FALSE, 0);
  gtk_widget_show (hbox);
364

365 366 367
  label = gtk_label_new_with_mnemonic (_("_Format:"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);
368

369 370 371
  combo = gimp_int_combo_box_new (NULL, 0);
  gtk_box_pack_start (GTK_BOX (hbox), combo, TRUE, TRUE, 0);
  gtk_widget_show (combo);
372

373
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
374

375 376 377 378 379
  for (i = 0; aa_formats[i]; i++)
    gimp_int_combo_box_append (GIMP_INT_COMBO_BOX (combo),
                               GIMP_INT_STORE_VALUE, i,
                               GIMP_INT_STORE_LABEL, aa_formats[i]->formatname,
                               -1);
380

381 382 383
  gimp_int_combo_box_connect (GIMP_INT_COMBO_BOX (combo), selected,
                              G_CALLBACK (gimp_int_combo_box_get_active),
                              &selected);
384

385
  gtk_widget_show (dialog);
386

387 388
  if (gimp_dialog_run (GIMP_DIALOG (dialog)) != GTK_RESPONSE_OK)
    selected = -1;
389

390
  gtk_widget_destroy (dialog);
391

392
  return selected;
393
}