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
#include <aalib.h>
34

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

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

40
/*
41 42
 * Declare some local functions.
 */
43 44 45 46 47 48 49 50 51 52 53 54 55 56
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);

57

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

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


71 72
MAIN ()

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

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

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

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

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

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

123
  return type;
124 125
}

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

141 142
  INIT_I18N ();

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

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

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

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

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

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

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

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

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

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

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

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

258
  return TRUE;
259 260
}

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

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

276 277 278
  drawable = gimp_drawable_get (drawable_ID);

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

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

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

  buffer = g_new (guchar, width * bpp);

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

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

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

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

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

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

325 326
  g_free (buffer);

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

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

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

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

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

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

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

358 359 360 361 362
  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);
363

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

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

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

374 375 376 377 378
  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);
379

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

384
  gtk_widget_show (dialog);
385

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

389
  gtk_widget_destroy (dialog);
390

391
  return selected;
392
}