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 43 44

#define SAVE_PROC      "file-aa-save"
#define PLUG_IN_BINARY "aa"


45
/*
46 47
 * Declare some local functions.
 */
48 49 50 51 52 53 54 55 56 57 58 59 60 61
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);

62

63
/*
64 65 66
 * Some global variables.
 */

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


76 77
MAIN ()

78
static void
79
query (void)
80
{
Sven Neumann's avatar
Sven Neumann committed
81
  static GimpParamDef save_args[] =
82
  {
83
    {GIMP_PDB_INT32,    "run-mode",     "Interactive, non-interactive"},
Sven Neumann's avatar
Sven Neumann committed
84 85 86
    {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"},
87 88
    {GIMP_PDB_STRING,   "raw-filename", "The name entered"},
    {GIMP_PDB_STRING,   "file-type",    "File type to use"}
89 90
  };

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

104 105
  gimp_register_file_handler_mime (SAVE_PROC, "text/plain");
  gimp_register_save_handler (SAVE_PROC, "txt,ansi,text", "");
106 107 108 109 110 111 112
}

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

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

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

128
  return type;
129 130
}

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

146 147
  INIT_I18N ();

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

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

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

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

        default:
          break;
        }
215
    }
216

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

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

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

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

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

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

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

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

263
  return TRUE;
264 265
}

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

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

281 282 283
  drawable = gimp_drawable_get (drawable_ID);

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

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

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

  buffer = g_new (guchar, width * bpp);

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

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

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

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

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

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

330 331
  g_free (buffer);

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

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

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

348
  /* Create the actual window. */
349
  dialog = gimp_dialog_new (_("Save as Text"), PLUG_IN_BINARY,
350
                         NULL, 0,
351
                         gimp_standard_help_func, SAVE_PROC,
352

353
                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
354
                         GTK_STOCK_SAVE,   GTK_RESPONSE_OK,
Sven Neumann's avatar
Sven Neumann committed
355

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

358 359 360 361 362
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                              GTK_RESPONSE_OK,
                                              GTK_RESPONSE_CANCEL,
                                              -1);

363 364 365 366 367
  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);
368

369 370 371
  label = gtk_label_new_with_mnemonic (_("_Format:"));
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);
372

373 374 375
  combo = gimp_int_combo_box_new (NULL, 0);
  gtk_box_pack_start (GTK_BOX (hbox), combo, TRUE, TRUE, 0);
  gtk_widget_show (combo);
376

377
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
378

379 380 381 382 383
  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);
384

385 386 387
  gimp_int_combo_box_connect (GIMP_INT_COMBO_BOX (combo), selected,
                              G_CALLBACK (gimp_int_combo_box_get_active),
                              &selected);
388

389
  gtk_widget_show (dialog);
390

391 392
  if (gimp_dialog_run (GIMP_DIALOG (dialog)) != GTK_RESPONSE_OK)
    selected = -1;
393

394
  gtk_widget_destroy (dialog);
395

396
  return selected;
397
}