aa.c 10.8 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 101
  gimp_plugin_menu_register ("file_aa_save", "<Save>");
  gimp_register_file_handler_mime ("file_aa_save", "text/plain");
102
  gimp_register_save_handler ("file_aa_save",
103
                              "txt,ansi,text",
104
                              "");
105 106 107 108 109 110 111
}

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

118 119 120 121 122
  while (*p && strcmp ((*p)->formatname, string))
    {
      p++;
      type++;
    }
123 124 125

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

127
  return type;
128 129
}

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

145 146
  INIT_I18N ();

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

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

Sven Neumann's avatar
Sven Neumann committed
176
  if (! (gimp_drawable_is_rgb (drawable_ID) ||
177
         gimp_drawable_is_gray (drawable_ID)))
178
    {
Sven Neumann's avatar
Sven Neumann committed
179
      status = GIMP_PDB_CALLING_ERROR;
180
    }
181

Sven Neumann's avatar
Sven Neumann committed
182
  if (status == GIMP_PDB_SUCCESS)
183
    {
184
      switch (run_mode)
185 186 187
        {
        case GIMP_RUN_INTERACTIVE:
          gimp_get_data ("file_aa_save", &output_type);
188
          output_type = aa_dialog (output_type);
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
          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;
        }
214
    }
215

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

228
  if (export == GIMP_EXPORT_EXPORT)
229
    gimp_image_delete (image_ID);
230 231

  values[0].data.d_status = status;
232 233 234 235 236 237
}

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

247 248
  format.width  = gimp_drawable_width (drawable_ID)  / 2;
  format.height = gimp_drawable_height (drawable_ID) / 2;
249

250
  /* Get a libaa context which will save its output to filename. */
251
  savedata.name   = filename;
252
  savedata.format = &format;
253

254
  context = aa_init (&save_d, &aa_defparams, &savedata);
255 256 257 258
  if (!context)
    return FALSE;

  gimp2aa (drawable_ID, context);
259 260
  aa_flush (context);
  aa_close (context);
261

262
  return TRUE;
263 264
}

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

  gint    width;
  gint    height;
  gint    x, y;
  gint    bpp;
277
  guchar *buffer;
Sven Neumann's avatar
Sven Neumann committed
278
  guchar *p;
279

280 281 282
  drawable = gimp_drawable_get (drawable_ID);

  width  = aa_imgwidth  (context);
283
  height = aa_imgheight (context);
284
  bpp    = drawable->bpp;
285

286 287 288 289
  gimp_tile_cache_ntiles ((width / gimp_tile_width ()) + 1);

  gimp_pixel_rgn_init (&pixel_rgn,
                       drawable, 0, 0, width, height,
290
                       FALSE, FALSE);
291 292 293

  buffer = g_new (guchar, width * bpp);

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

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

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

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

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

        default:
          g_assert_not_reached ();
          break;
        }
327
    }
328

329 330
  g_free (buffer);

331 332
  renderparams = aa_getrenderparams ();
  renderparams->dither = AA_FLOYD_S;
333

334
  aa_render (context, renderparams, 0, 0,
335
             aa_scrwidth (context), aa_scrheight (context));
336 337
}

338
static gint
339
aa_dialog (gint selected)
340
{
341 342 343 344 345
  GtkWidget *dialog;
  GtkWidget *hbox;
  GtkWidget *label;
  GtkWidget *combo;
  gint       i;
346

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

352 353
                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                         GTK_STOCK_OK,     GTK_RESPONSE_OK,
Sven Neumann's avatar
Sven Neumann committed
354

355
                         NULL);
Sven Neumann's avatar
Sven Neumann committed
356

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

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

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

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

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

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

383
  gtk_widget_show (dialog);
384

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

388
  gtk_widget_destroy (dialog);
389

390
  return selected;
391
}