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
                          "Tim Newsome <nuisance@cmu.edu>",
                          "Tim Newsome <nuisance@cmu.edu>",
                          "1997",
100
                          N_("ASCII art"),
101 102 103 104
                          "RGB*, GRAY*",
                          GIMP_PLUGIN,
                          G_N_ELEMENTS (save_args), 0,
                          save_args, NULL);
105

106 107
  gimp_plugin_menu_register ("file_aa_save", "<Save>");
  gimp_register_file_handler_mime ("file_aa_save", "text/plain");
108
  gimp_register_save_handler ("file_aa_save",
109
                              "txt,ansi,text",
110
                              "");
111 112 113 114 115 116 117
}

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

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

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

133
  return type;
134 135
}

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

151 152
  INIT_I18N ();

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

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

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

Sven Neumann's avatar
Sven Neumann committed
188
  if (status == GIMP_PDB_SUCCESS)
189
    {
190
      switch (run_mode)
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 218 219
        {
        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;
        }
220
    }
221

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

234
  if (export == GIMP_EXPORT_EXPORT)
235
    gimp_image_delete (image_ID);
236 237

  values[0].data.d_status = status;
238 239 240 241 242 243
}

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

253 254
  format.width  = gimp_drawable_width (drawable_ID)  / 2;
  format.height = gimp_drawable_height (drawable_ID) / 2;
255

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

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

  gimp2aa (drawable_ID, context);
265 266
  aa_flush (context);
  aa_close (context);
267

268
  return TRUE;
269 270
}

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

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

286 287 288
  drawable = gimp_drawable_get (drawable_ID);

  width  = aa_imgwidth  (context);
289
  height = aa_imgheight (context);
290
  bpp    = drawable->bpp;
291

292 293 294 295
  gimp_tile_cache_ntiles ((width / gimp_tile_width ()) + 1);

  gimp_pixel_rgn_init (&pixel_rgn,
                       drawable, 0, 0, width, height,
296
                       FALSE, FALSE);
297 298 299

  buffer = g_new (guchar, width * bpp);

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

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

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

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

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

        default:
          g_assert_not_reached ();
          break;
        }
333
    }
334

335 336
  g_free (buffer);

337 338
  renderparams = aa_getrenderparams ();
  renderparams->dither = AA_FLOYD_S;
339

340
  aa_render (context, renderparams, 0, 0,
341
             aa_scrwidth (context), aa_scrheight (context));
342 343
}

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

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

358 359
                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                         GTK_STOCK_OK,     GTK_RESPONSE_OK,
Sven Neumann's avatar
Sven Neumann committed
360

361
                         NULL);
Sven Neumann's avatar
Sven Neumann committed
362

363
  /*  file save type  */
364 365
  frame = gimp_frame_new (_("Data Formatting"));
  gtk_container_set_border_width (GTK_CONTAINER (frame), 12);
366
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), frame, TRUE, TRUE, 0);
367

368
  toggle_vbox = gtk_vbox_new (FALSE, 6);
369
  gtk_container_add (GTK_CONTAINER (frame), toggle_vbox);
370

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

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

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

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

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

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

398 399
  gtk_widget_show (dlg);

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

  gtk_widget_destroy (dlg);
404 405

  return selected_type;
406 407
}

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