gpb.c 35.7 KB
Newer Older
1
/* Plug-in to save .gpb (GIMP Pixmap Brush) files.
2
3
 * The format for pixmap brushes might well change, and this will have to
 * be updated.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 * Copyright (C) 1999 Tor Lillqvist
 *
 * 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.
 */

Tor Lillqvist's avatar
Tor Lillqvist committed
22
23
24
25
26
/* set to the level of debugging output you want, 0 for none */
#define GPB_DEBUG 0

#define IFDBG(level) if (GPB_DEBUG >= level)

27
28
29
30
#include "config.h"

#include <stdio.h>
#include <string.h>
Tor Lillqvist's avatar
Tor Lillqvist committed
31
#include <stdlib.h>
32
33
34

#include <gtk/gtk.h>
#include <libgimp/gimp.h>
35
#include <libgimp/gimpui.h>
36
#include <libgimp/parasiteio.h>
37
38
39
40

#include "app/brush_header.h"
#include "app/pattern_header.h"

Tor Lillqvist's avatar
Tor Lillqvist committed
41
#define DUMMY_PATTERN_NAME "x"
42

Tor Lillqvist's avatar
Tor Lillqvist committed
43
#define MAXDESCLEN 256
44

Tor Lillqvist's avatar
Tor Lillqvist committed
45
46
47
48
49
50
51
52
53
54
/* Parameters applicable each time we save a gpb or gih, saved
 * in the main gimp application between invocations of this plug-in.
 */
static struct {
  /* Use by both gpb and gih: */
  guint spacing;
  guchar description[MAXDESCLEN+1];
} info =
/* Initialize to this, change if non-interactive later */
{  
55
56
57
58
  10,
  "GIMP Pixmap Brush"
};

Tor Lillqvist's avatar
Tor Lillqvist committed
59
60
61
static gint run_flag = 0;
static gint num_layers_with_alpha;

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
static PixPipeParams gihparms;

typedef struct
{
  GOrientation orientation;
  gint32 image;
  gint32 toplayer;
  gint nguides;
  gint32 *guides;
  gint *value;
  GtkWidget *count_label;	/* Corresponding count adjustment, */
  gint *count;			/* cols or rows */
  gint *other_count;		/* And the other one */
  GtkObject *ncells;
  GtkObject *rank0;
  GtkWidget *warning_label;
} SizeAdjustmentData;

80
81
/* static gint32 *vguides, *hguides;       */
/* static gint nvguides = 0, nhguides = 0; */
82
83
84

/* Declare some local functions.
 */
85
86
87
88
89
90
91
92
93
static void   query    (void);
static void   run      (char    *name,
			int      nparams,
			GParam  *param,
			int     *nreturn_vals,
			GParam **return_vals);

static void   init_gtk (void);

94
95
96
97
98
99
100
101
102
103
104
105
106
107

GPlugInInfo PLUG_IN_INFO =
{
  NULL,    /* init_proc */
  NULL,    /* quit_proc */
  query,   /* query_proc */
  run,     /* run_proc */
};

MAIN ()

static void
query ()
{
Tor Lillqvist's avatar
Tor Lillqvist committed
108
  static GParamDef gpb_save_args[] =
109
  {
110
111
112
113
114
115
116
    { PARAM_INT32,    "run_mode",     "Interactive, non-interactive" },
    { PARAM_IMAGE,    "image",        "Input image" },
    { PARAM_DRAWABLE, "drawable",     "Drawable to save" },
    { PARAM_STRING,   "filename",     "The name of the file to save the brush in" },
    { PARAM_STRING,   "raw_filename", "The name of the file to save the brush in" },
    { PARAM_INT32,    "spacing",      "Spacing of the brush" },
    { PARAM_STRING,   "description",  "Short description of the brush" },
117
  };
118
  static int ngpb_save_args = sizeof (gpb_save_args) / sizeof (gpb_save_args[0]);
Tor Lillqvist's avatar
Tor Lillqvist committed
119
120
121

  static GParamDef gih_save_args[] =
  {
122
123
124
125
126
127
128
    { PARAM_INT32,    "run_mode",     "Interactive, non-interactive" },
    { PARAM_IMAGE,    "image",        "Input image" },
    { PARAM_DRAWABLE, "drawable",     "Drawable to save" },
    { PARAM_STRING,   "filename",     "The name of the file to save the brush pipe in" },
    { PARAM_STRING,   "raw_filename", "The name of the file to save the brush pipe in" },
    { PARAM_INT32,    "spacing",      "Spacing of the brush" },
    { PARAM_STRING,   "description",  "Short description of the brush pipe" },
Tor Lillqvist's avatar
Tor Lillqvist committed
129
  };
130
  static int ngih_save_args = sizeof (gih_save_args) / sizeof (gih_save_args[0]);
131
132

  gimp_install_procedure ("file_gpb_save",
Tor Lillqvist's avatar
Tor Lillqvist committed
133
134
135
136
			  "saves images in GIMP pixmap brush format", 
                          "This plug-in saves a layer of an image in "
			  "the GIMP pixmap brush format. "
                          "The image must have an alpha channel.",
137
138
139
140
141
142
                          "Tor Lillqvist",
                          "Tor Lillqvist",
                          "1999",
                          "<Save>/GPB",
			  "RGBA",
                          PROC_PLUG_IN,
Tor Lillqvist's avatar
Tor Lillqvist committed
143
144
                          ngpb_save_args, 0,
                          gpb_save_args, NULL);
145
146

  gimp_register_save_handler ("file_gpb_save", "gpb", "");
Tor Lillqvist's avatar
Tor Lillqvist committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

  gimp_install_procedure ("file_gih_save",
			  "saves images in GIMP pixmap brush pipe format", 
                          "This plug-in saves an image in "
			  "the GIMP pixmap brush pipe format. "
                          "The image must have an alpha channel."
			  "The image can be multi-layered, "
			  "and additionally the layers can be divided into "
			  "a rectangular array of brushes.",
                          "Tor Lillqvist",
                          "Tor Lillqvist",
                          "1999",
                          "<Save>/GIH",
			  "RGBA",
                          PROC_PLUG_IN,
                          ngih_save_args, 0,
                          gih_save_args, NULL);

  gimp_register_save_handler ("file_gih_save", "gih", "");
}

168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
static void 
init_gtk ()
{
  gchar **argv;
  gint argc;

  argc = 1;
  argv = g_new (gchar *, 1);
  argv[0] = g_strdup ("gpb_save");
  
  gtk_init (&argc, &argv);
  gtk_rc_parse (gimp_gtkrc ());
}


Tor Lillqvist's avatar
Tor Lillqvist committed
183
184
185
186
187
static void
adjustment_callback (GtkWidget *widget,
		     gpointer   data)
{
  *((gint *) data) = GTK_ADJUSTMENT (widget)->value;
188
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
static void
size_adjustment_callback (GtkWidget *widget,
			  gpointer   data)
{
  /* Unfortunately this doesn't work, sigh. The guides don't show up unless
   * you manually force a redraw of the image.
   */
  int i;
  int size;
  int newn;
  SizeAdjustmentData *adj = (SizeAdjustmentData *) data;
  char buf[10];

  for (i = 0; i < adj->nguides; i++)
    gimp_image_delete_guide (adj->image, adj->guides[i]);
  g_free (adj->guides);
  adj->guides = NULL;
  gimp_displays_flush ();

  *(adj->value) = GTK_ADJUSTMENT (widget)->value;

  if (adj->orientation == ORIENTATION_VERTICAL)
    {
      size = gimp_image_width (adj->image);
      newn = size / *(adj->value);
      adj->nguides = newn - 1;
      adj->guides = g_new (gint32, adj->nguides);
      for (i = 0; i < adj->nguides; i++)
	adj->guides[i] = gimp_image_add_vguide (adj->image,
						*(adj->value) * (i+1));
    }
  else
    {
      size = gimp_image_height (adj->image);
      newn = size / *(adj->value);
      adj->nguides = newn - 1;
      adj->guides = g_new (gint32, adj->nguides);
      for (i = 0; i < adj->nguides; i++)
	adj->guides[i] = gimp_image_add_hguide (adj->image,
						*(adj->value) * (i+1));
    }
  gimp_displays_flush ();
  sprintf (buf, "%2d", newn);
  gtk_label_set_text (GTK_LABEL (adj->count_label), buf);
  *(adj->count) = newn;
  if (newn * *(adj->value) != size)
    gtk_widget_show (GTK_WIDGET (adj->warning_label));
  else
    gtk_widget_hide (GTK_WIDGET (adj->warning_label));
  if (adj->ncells != NULL)
    gtk_adjustment_set_value (GTK_ADJUSTMENT (adj->ncells),
			      *(adj->other_count) * *(adj->count));
  if (adj->rank0 != NULL)
    gtk_adjustment_set_value (GTK_ADJUSTMENT (adj->rank0),
			      *(adj->other_count) * *(adj->count));
			    
}

248
static void
Tor Lillqvist's avatar
Tor Lillqvist committed
249
250
entry_callback (GtkWidget *widget,
		gpointer   data)
251
{
Tor Lillqvist's avatar
Tor Lillqvist committed
252
253
254
255
256
  if (data == info.description)
    {
      strncpy (info.description, gtk_entry_get_text (GTK_ENTRY (widget)), MAXDESCLEN);
      info.description[MAXDESCLEN]  = 0;
    }
257
258
}

259
260
261
262
263
264
265
static void
cb_callback (GtkWidget *widget,
	     gpointer   data)
{
  *((char **) data) = gtk_entry_get_text (GTK_ENTRY (widget));
}

266
267
268
269
static void
close_callback (GtkWidget *widget,
		gpointer   data)
{
Tor Lillqvist's avatar
Tor Lillqvist committed
270
  gtk_main_quit ();
271
272
273
274
275
276
}

static void
ok_callback (GtkWidget *widget,
	     gpointer    data)
{
277
278
279
280
281
  int i;

  for (i = 0; i < PIXPIPE_MAXDIM; i++)
    gihparms.selection[i] = g_strdup (gihparms.selection[i]);

282
283
284
285
  run_flag = 1;
  gtk_widget_destroy (GTK_WIDGET (data));
}

Tor Lillqvist's avatar
Tor Lillqvist committed
286
287
288
static void
common_save_dialog (GtkWidget *dlg,
		    GtkWidget *table)
289
{
Sven Neumann's avatar
Sven Neumann committed
290
  GtkWidget *hbbox;
291
292
  GtkWidget *button;
  GtkWidget *label;
Tor Lillqvist's avatar
Tor Lillqvist committed
293
294
295
  GtkObject *adjustment;
  GtkWidget *box;
  GtkWidget *spinbutton;
296
297
298
  GtkWidget *entry;

  /*  Action area  */
Sven Neumann's avatar
Sven Neumann committed
299
300
301
302
303
304
305
  gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (dlg)->action_area), 2);
  gtk_box_set_homogeneous (GTK_BOX (GTK_DIALOG (dlg)->action_area), FALSE);
  hbbox = gtk_hbutton_box_new ();
  gtk_button_box_set_spacing (GTK_BUTTON_BOX (hbbox), 4);
  gtk_box_pack_end (GTK_BOX (GTK_DIALOG (dlg)->action_area), hbbox, FALSE, FALSE, 0);
  gtk_widget_show (hbbox);
 
306
307
  button = gtk_button_new_with_label ("OK");
  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
Tor Lillqvist's avatar
Tor Lillqvist committed
308
  gtk_signal_connect (GTK_OBJECT (button), "clicked",
Sven Neumann's avatar
Sven Neumann committed
309
310
311
		      (GtkSignalFunc) ok_callback,
		      dlg);
  gtk_box_pack_start (GTK_BOX (hbbox), button, FALSE, FALSE, 0);
312
313
314
315
  gtk_widget_grab_default (button);
  gtk_widget_show (button);

  button = gtk_button_new_with_label ("Cancel");
Tor Lillqvist's avatar
Tor Lillqvist committed
316
317
  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
318
319
			     (GtkSignalFunc) gtk_widget_destroy,
			     GTK_OBJECT (dlg));
Sven Neumann's avatar
Sven Neumann committed
320
  gtk_box_pack_start (GTK_BOX (hbbox), button, FALSE, FALSE, 0);
Tor Lillqvist's avatar
Tor Lillqvist committed
321
  gtk_widget_show (button);
322
323
324
325

  gtk_table_set_row_spacings (GTK_TABLE (table), 10);
  gtk_table_set_col_spacings (GTK_TABLE (table), 10);

Tor Lillqvist's avatar
Tor Lillqvist committed
326
327
328
329
330
331
332
  /*
   * Spacing: __
   */
  gtk_table_resize (GTK_TABLE (table),
		    GTK_TABLE (table)->nrows + 1, GTK_TABLE (table)->ncols);

  label = gtk_label_new ("Spacing (percent):");
333
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
Tor Lillqvist's avatar
Tor Lillqvist committed
334
335
336
  gtk_table_attach (GTK_TABLE (table), label, 0, 1,
		    GTK_TABLE (table)->nrows - 1, GTK_TABLE (table)->nrows,
		    GTK_FILL, GTK_FILL, 0, 0);
337
338
  gtk_widget_show (label);

Tor Lillqvist's avatar
Tor Lillqvist committed
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
  adjustment = gtk_adjustment_new (info.spacing, 1, 1000, 1, 10, 10);
  spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (adjustment), 1, 0);
  gtk_spin_button_set_shadow_type (GTK_SPIN_BUTTON (spinbutton),
				   GTK_SHADOW_NONE);
  gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbutton), TRUE);
  gtk_widget_set_usize (spinbutton, 75, 0);
  box = gtk_hbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (box), spinbutton, FALSE, TRUE, 0);
  gtk_table_attach (GTK_TABLE (table), box, 1, 2, 
		    GTK_TABLE (table)->nrows - 1, GTK_TABLE (table)->nrows,
		    GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
		      (GtkSignalFunc) adjustment_callback, &info.spacing);
  gtk_widget_show (spinbutton);
  gtk_widget_show (box);

  /*
   * Description: ___________
   */
  gtk_table_resize (GTK_TABLE (table),
		    GTK_TABLE (table)->nrows + 1, GTK_TABLE (table)->ncols);
360
361
362

  label = gtk_label_new ("Description:");
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
Tor Lillqvist's avatar
Tor Lillqvist committed
363
364
365
  gtk_table_attach (GTK_TABLE (table), label, 0, 1,
		    GTK_TABLE (table)->nrows - 1, GTK_TABLE (table)->nrows,
		    GTK_FILL, GTK_FILL, 0, 0);
366
367
368
  gtk_widget_show (label);

  entry = gtk_entry_new ();
Tor Lillqvist's avatar
Tor Lillqvist committed
369
370
371
  gtk_table_attach (GTK_TABLE (table), entry, 1, 2,
		    GTK_TABLE (table)->nrows - 1, GTK_TABLE (table)->nrows,
		    GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
372
373
374
  gtk_widget_set_usize (entry, 200, 0);
  gtk_entry_set_text (GTK_ENTRY (entry), info.description);
  gtk_signal_connect (GTK_OBJECT (entry), "changed",
Tor Lillqvist's avatar
Tor Lillqvist committed
375
		      (GtkSignalFunc) entry_callback, info.description);
376
  gtk_widget_show (entry);
Tor Lillqvist's avatar
Tor Lillqvist committed
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
}

static gint
gpb_save_dialog ()
{
  GtkWidget *dlg;
  GtkWidget *table;

  dlg = gtk_dialog_new ();
  gtk_window_set_title (GTK_WINDOW (dlg), "Save As Pixmap Brush");
  gtk_window_position (GTK_WINDOW (dlg), GTK_WIN_POS_MOUSE);
  gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
		      (GtkSignalFunc) close_callback, NULL);

  /* The main table */
  table = gtk_table_new (2, 0, FALSE);
  gtk_container_border_width (GTK_CONTAINER (table), 10);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), table, TRUE, TRUE, 0);
  gtk_widget_show (table);

  common_save_dialog (dlg, table);
398
399
400
401
402
403
404
405
406

  gtk_widget_show (dlg);

  gtk_main ();
  gdk_flush ();

  return run_flag;
}

Tor Lillqvist's avatar
Tor Lillqvist committed
407
static gint
408
gih_save_dialog (gint32 image_ID)
Tor Lillqvist's avatar
Tor Lillqvist committed
409
410
{
  GtkWidget *dlg;
411
  GtkWidget *table, *dimtable;
Tor Lillqvist's avatar
Tor Lillqvist committed
412
413
414
415
  GtkWidget *label;
  GtkObject *adjustment;
  GtkWidget *spinbutton;
  GtkWidget *box;
416
417
  GtkWidget *cb;
  GList *cbitems = NULL;
Tor Lillqvist's avatar
Tor Lillqvist committed
418
419
  gint i;
  gchar buffer[100];
420
421
422
  SizeAdjustmentData cellw_adjust, cellh_adjust;
  gint32 *layer_ID;
  gint32 nlayers;
Tor Lillqvist's avatar
Tor Lillqvist committed
423
424
425
426
427

  /* Setup default values */
  if (gihparms.rows >= 1 && gihparms.cols >= 1)
    gihparms.ncells = num_layers_with_alpha * gihparms.rows * gihparms.cols;
  else
428
429
430
431
432
433
434
    gihparms.ncells = num_layers_with_alpha;

  if (gihparms.cellwidth == 1 && gihparms.cellheight == 1)
    {
      gihparms.cellwidth = gimp_image_width (image_ID) / gihparms.cols;
      gihparms.cellheight = gimp_image_height (image_ID) / gihparms.rows;
    }
Tor Lillqvist's avatar
Tor Lillqvist committed
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449

  dlg = gtk_dialog_new ();
  gtk_window_set_title (GTK_WINDOW (dlg), "Save As Pixmap Brush Pipe");
  gtk_window_position (GTK_WINDOW (dlg), GTK_WIN_POS_MOUSE);
  gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
		      (GtkSignalFunc) close_callback, NULL);

  /* The main table */
  table = gtk_table_new (2, 0, FALSE);
  gtk_container_border_width (GTK_CONTAINER (table), 10);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), table, TRUE, TRUE, 0);
  gtk_widget_show (table);

  common_save_dialog (dlg, table);

450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
  /*
   * Cell size: __ x __ pixels
   */
  
  gtk_table_resize (GTK_TABLE (table),
		    GTK_TABLE (table)->nrows + 1, GTK_TABLE (table)->ncols);

  label = gtk_label_new ("Cell size:");
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1,
		    GTK_TABLE (table)->nrows - 1, GTK_TABLE (table)->nrows,
		    GTK_FILL, GTK_FILL, 0, 0);
  gtk_widget_show (label);

  box = gtk_hbox_new (FALSE, 0);
  
  adjustment = gtk_adjustment_new (gihparms.cellwidth,
				   2, gimp_image_width (image_ID), 1, 1, 1);
  spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (adjustment), 1, 0);
  gtk_spin_button_set_shadow_type (GTK_SPIN_BUTTON (spinbutton),
				    GTK_SHADOW_NONE);
  gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbutton), TRUE);
  gtk_widget_set_usize (spinbutton, 75, 0);
  gtk_box_pack_start (GTK_BOX (box), spinbutton, FALSE, FALSE, 0);

  layer_ID = gimp_image_get_layers (image_ID, &nlayers);
  cellw_adjust.orientation = ORIENTATION_VERTICAL;
  cellw_adjust.image = image_ID;
  cellw_adjust.toplayer = layer_ID[nlayers-1];
  cellw_adjust.nguides = 0;
  cellw_adjust.guides = NULL;
  cellw_adjust.value = &gihparms.cellwidth;
  gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
		      (GtkSignalFunc) size_adjustment_callback,
		      &cellw_adjust);
  gtk_widget_show (spinbutton);

  label = gtk_label_new (" x ");
  gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
  gtk_widget_show (label);
  
  adjustment = gtk_adjustment_new (gihparms.cellheight,
				   2, gimp_image_height (image_ID), 1, 1, 1);
  spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (adjustment), 1, 0);
  gtk_spin_button_set_shadow_type (GTK_SPIN_BUTTON (spinbutton),
				    GTK_SHADOW_NONE);
  gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbutton), TRUE);
  gtk_widget_set_usize (spinbutton, 75, 0);
  gtk_box_pack_start (GTK_BOX (box), spinbutton, FALSE, FALSE, 0);
  cellh_adjust.orientation = ORIENTATION_HORIZONTAL;
  cellh_adjust.image = image_ID;
  cellh_adjust.toplayer = layer_ID[nlayers-1];
  cellh_adjust.nguides = 0;
  cellh_adjust.guides = NULL;
  cellh_adjust.value = &gihparms.cellheight;
  gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
		      (GtkSignalFunc) size_adjustment_callback,
		      &cellh_adjust);
  gtk_widget_show (spinbutton);

  label = gtk_label_new (" pixels");
  gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
  gtk_widget_show (label);
  
  gtk_table_attach (GTK_TABLE (table), box, 1, 2,
		    GTK_TABLE (table)->nrows - 1, GTK_TABLE (table)->nrows,
		    GTK_FILL, GTK_FILL, 0, 0);
  gtk_widget_show (box);

  g_free (layer_ID);

521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
  /*
   * Number of cells: ___
   */

  gtk_table_resize (GTK_TABLE (table),
		    GTK_TABLE (table)->nrows + 1, GTK_TABLE (table)->ncols);

  label = gtk_label_new ("Number of cells:");
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1,
		    GTK_TABLE (table)->nrows - 1, GTK_TABLE (table)->nrows,
		    GTK_FILL, GTK_FILL, 0, 0);
  gtk_widget_show (label);

  adjustment = gtk_adjustment_new (gihparms.ncells, 1, 1000, 1, 10, 10);
  spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (adjustment), 1, 0);
  gtk_spin_button_set_shadow_type (GTK_SPIN_BUTTON (spinbutton),
				   GTK_SHADOW_NONE);
  gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbutton), TRUE);
  gtk_widget_set_usize (spinbutton, 75, 0);
  box = gtk_hbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (box), spinbutton, FALSE, TRUE, 0);
  gtk_table_attach (GTK_TABLE (table), box, 1, 2,
		    GTK_TABLE (table)->nrows - 1, GTK_TABLE (table)->nrows,
		    GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
		      (GtkSignalFunc) adjustment_callback, &gihparms.ncells);
  gtk_widget_show (spinbutton);
  gtk_widget_show (box);

551
552
553
554
555
  if (gihparms.dim == 1)
    cellw_adjust.ncells = cellh_adjust.ncells = adjustment;
  else
    cellw_adjust.ncells = cellh_adjust.ncells = NULL;

Tor Lillqvist's avatar
Tor Lillqvist committed
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
  /*
   * Display as: __ rows x __ cols
   */
  gtk_table_resize (GTK_TABLE (table),
		    GTK_TABLE (table)->nrows + 1, GTK_TABLE (table)->ncols);

  label = gtk_label_new ("Display as:");
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1,
		    GTK_TABLE (table)->nrows - 1, GTK_TABLE (table)->nrows,
		    GTK_FILL, GTK_FILL, 0, 0);
  gtk_widget_show (label);

  box = gtk_hbox_new (FALSE, 0);
  
571
572
573
574
575
576
577
  sprintf (buffer, "%2d", gihparms.rows);
  label = gtk_label_new (buffer);
  gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
  cellh_adjust.count_label = label;
  cellh_adjust.count = &gihparms.rows;
  cellh_adjust.other_count = &gihparms.cols;
  gtk_widget_show (label);
Tor Lillqvist's avatar
Tor Lillqvist committed
578
579
580
581
582

  label = gtk_label_new (" rows of ");
  gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
  gtk_widget_show (label);
  
583
584
585
586
587
588
589
  sprintf (buffer, "%2d", gihparms.cols);
  label = gtk_label_new (buffer);
  gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
  cellw_adjust.count_label = label;
  cellw_adjust.count = &gihparms.cols;  
  cellw_adjust.other_count = &gihparms.rows;
  gtk_widget_show (label);
Tor Lillqvist's avatar
Tor Lillqvist committed
590

591
  label = gtk_label_new (" columns on each layer");
Tor Lillqvist's avatar
Tor Lillqvist committed
592
593
594
  gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
  gtk_widget_show (label);
  
595
596
597
598
599
600
601
602
  label = gtk_label_new (" (width mismatch!) ");
  gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
  cellw_adjust.warning_label = label;
  
  label = gtk_label_new (" (height mismatch!) ");
  gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
  cellh_adjust.warning_label = label;
  
Tor Lillqvist's avatar
Tor Lillqvist committed
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
  gtk_table_attach (GTK_TABLE (table), box, 1, 2,
		    GTK_TABLE (table)->nrows - 1, GTK_TABLE (table)->nrows,
		    GTK_FILL, GTK_FILL, 0, 0);
  gtk_widget_show (box);
  
  /*
   * Dimension: ___
   */

  gtk_table_resize (GTK_TABLE (table),
		    GTK_TABLE (table)->nrows + 1, GTK_TABLE (table)->ncols);

  label = gtk_label_new ("Dimension:");
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1,
		    GTK_TABLE (table)->nrows - 1, GTK_TABLE (table)->nrows,
		    GTK_FILL, GTK_FILL, 0, 0);
  gtk_widget_show (label);

  adjustment = gtk_adjustment_new (gihparms.dim, 1, 5, 1, 1, 1);
  spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (adjustment), 1, 0);
  gtk_spin_button_set_shadow_type (GTK_SPIN_BUTTON (spinbutton),
				    GTK_SHADOW_NONE);
  gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbutton), TRUE);
  gtk_widget_set_usize (spinbutton, 75, 0);
  box = gtk_hbox_new (FALSE, 0);
  gtk_box_pack_start (GTK_BOX (box), spinbutton, FALSE, FALSE, 0);
  gtk_table_attach (GTK_TABLE (table), box, 1, 2,
		    GTK_TABLE (table)->nrows - 1, GTK_TABLE (table)->nrows,
		    GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
		      (GtkSignalFunc) adjustment_callback, &gihparms.dim);
  gtk_widget_show (spinbutton);
  gtk_widget_show (box);

  /*
   * Ranks: __ __ __ __ __
   */

  gtk_table_resize (GTK_TABLE (table),
		    GTK_TABLE (table)->nrows + 1, GTK_TABLE (table)->ncols);

  label = gtk_label_new ("Ranks:");
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1,
		    GTK_TABLE (table)->nrows - 1, GTK_TABLE (table)->nrows,
		    GTK_FILL, GTK_FILL, 0, 0);
  gtk_widget_show (label);

652
653
  dimtable = gtk_table_new (PIXPIPE_MAXDIM, 1, FALSE);
  for (i = 0; i < PIXPIPE_MAXDIM; i++)
Tor Lillqvist's avatar
Tor Lillqvist committed
654
655
656
657
658
659
660
    {
      adjustment = gtk_adjustment_new (gihparms.rank[i], 0, 100, 1, 1, 1);
      spinbutton = gtk_spin_button_new (GTK_ADJUSTMENT (adjustment), 1, 0);
      gtk_spin_button_set_shadow_type (GTK_SPIN_BUTTON (spinbutton),
				       GTK_SHADOW_NONE);
      gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (spinbutton), TRUE);
      gtk_widget_set_usize (spinbutton, 75, 0);
661
662
663
664
      box = gtk_hbox_new (FALSE, 0);
      gtk_box_pack_start (GTK_BOX (box), spinbutton, FALSE, TRUE, 0);
      gtk_table_attach (GTK_TABLE (dimtable), box, i, i + 1, 0, 1,
			GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 3, 0);
Tor Lillqvist's avatar
Tor Lillqvist committed
665
666
667
      gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
			  (GtkSignalFunc) adjustment_callback, &gihparms.rank[i]);
      gtk_widget_show (spinbutton);
668
      gtk_widget_show (box);
669
      if (i == 0)
670
671
672
673
674
675
	{ 
	  if (gihparms.dim == 1)
	    cellw_adjust.rank0 = cellh_adjust.rank0 = adjustment;
	  else
	    cellw_adjust.rank0 = cellh_adjust.rank0 = NULL;
	}
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
    }
  gtk_table_attach (GTK_TABLE (table), dimtable, 1, 2,
		    GTK_TABLE (table)->nrows - 1, GTK_TABLE (table)->nrows,
		    GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
  gtk_widget_show (dimtable);

  /*
   * Selection: ______ ______ ______ ______ ______
   */
  
  gtk_table_resize (GTK_TABLE (table),
		    GTK_TABLE (table)->nrows + 1, GTK_TABLE (table)->ncols);

  label = gtk_label_new ("Selection:");
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1,
		    GTK_TABLE (table)->nrows - 1, GTK_TABLE (table)->nrows,
		    GTK_FILL, GTK_FILL, 0, 0);
  gtk_widget_show (label);

  cbitems = g_list_append (cbitems, "incremental");
  cbitems = g_list_append (cbitems, "angular");
  cbitems = g_list_append (cbitems, "random");
  cbitems = g_list_append (cbitems, "velocity");
  cbitems = g_list_append (cbitems, "pressure");
  cbitems = g_list_append (cbitems, "xtilt");
  cbitems = g_list_append (cbitems, "ytilt");

  box = gtk_hbox_new (FALSE, 0);
705
  for (i = 0; i < PIXPIPE_MAXDIM; i++)
706
707
708
709
710
    {
      cb = gtk_combo_new ();
      gtk_combo_set_popdown_strings (GTK_COMBO (cb), cbitems);
      if (gihparms.selection[i])
	gtk_entry_set_text (GTK_ENTRY (GTK_COMBO (cb)->entry), gihparms.selection[i]);
Tor Lillqvist's avatar
Tor Lillqvist committed
711
712
      else
	gtk_entry_set_text (GTK_ENTRY (GTK_COMBO (cb)->entry), "random");
713
714
715
716
717
718
      gtk_entry_set_editable (GTK_ENTRY (GTK_COMBO (cb)->entry), FALSE);

      gtk_box_pack_start (GTK_BOX (box), cb, FALSE, TRUE, 2);
      gtk_signal_connect (GTK_OBJECT (GTK_COMBO (cb)->entry), "changed",
			  (GtkSignalFunc) cb_callback, &gihparms.selection[i]);
      gtk_widget_show (cb);
Tor Lillqvist's avatar
Tor Lillqvist committed
719
    }
720

Tor Lillqvist's avatar
Tor Lillqvist committed
721
722
723
724
  gtk_table_attach (GTK_TABLE (table), box, 1, 2,
		    GTK_TABLE (table)->nrows - 1, GTK_TABLE (table)->nrows,
		    GTK_SHRINK, GTK_EXPAND | GTK_FILL, 0, 0);
  gtk_widget_show (box);
725
  
Tor Lillqvist's avatar
Tor Lillqvist committed
726
727
728
729
730
  gtk_widget_show (dlg);

  gtk_main ();
  gdk_flush ();

731
732
733
734
735
  for (i = 0; i < cellw_adjust.nguides; i++)
    gimp_image_delete_guide (image_ID, cellw_adjust.guides[i]);
  for (i = 0; i < cellh_adjust.nguides; i++)
    gimp_image_delete_guide (image_ID, cellh_adjust.guides[i]);

Tor Lillqvist's avatar
Tor Lillqvist committed
736
737
738
739
740
741
742
743
744
745
  if (run_flag)
    {
      /* Fix up bogus values */
      gihparms.ncells =
	MIN (gihparms.ncells,
	     num_layers_with_alpha * gihparms.rows * gihparms.cols);
    }
  return run_flag;
}

746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
static gboolean
try_fwrite (gpointer buffer,
	    guint   size,
	    guint   nitems,
	    FILE   *file)
{
  if (fwrite (buffer, size, nitems, file) < nitems)
    {
      g_message ("GPB: write error");
      fclose (file);
      return FALSE;
    }
  return TRUE;
}

static gboolean
Tor Lillqvist's avatar
Tor Lillqvist committed
762
763
764
765
save_one_gpb (FILE      *file,
	      GPixelRgn *pixel_rgn,
	      int	 index,
	      int        total)
766
767
768
{
  BrushHeader brushheader;
  PatternHeader patternheader;
Tor Lillqvist's avatar
Tor Lillqvist committed
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
  guint y, x;
  guchar *buffer;
  guint width, height;

  width = pixel_rgn->w;
  height = pixel_rgn->h;

  brushheader.header_size = g_htonl (sizeof (brushheader) + strlen (info.description) + 1);
  brushheader.version = g_htonl (2);
  brushheader.width = g_htonl (width);
  brushheader.height = g_htonl (height);
  brushheader.bytes = g_htonl (1);
  brushheader.magic_number = g_htonl (GBRUSH_MAGIC);
  brushheader.spacing = g_htonl (info.spacing);

  if (!try_fwrite (&brushheader, sizeof (brushheader), 1, file))
    return FALSE;
  if (!try_fwrite (info.description, strlen (info.description) + 1, 1, file))
    return FALSE;

  IFDBG(3) g_message ("saving gpb %d of %d: %dx%d, offset %d,%d stride %d drawable %d ",
		      index+1, total,
		      width, height, pixel_rgn->x, pixel_rgn->y,
		      pixel_rgn->rowstride, pixel_rgn->drawable->id);
  
  buffer = g_malloc (width * pixel_rgn->bpp);
  for (y = 0; y < height; y++)
    {
#if 0
      /* No; the brushes don't have to be the same size, of course. */
      if (y >= pixel_rgn->h)
	{
	  if (y == pixel_rgn->h)
	    memset (buffer, 0, width);
	  if (!try_fwrite (buffer, width, 1, file))
	    {
	      g_free (buffer);
	      return FALSE;
	    }
	}
      else
#endif
	{
	  gimp_pixel_rgn_get_row (pixel_rgn, buffer,
				  0 + pixel_rgn->x, y + pixel_rgn->y,
				  pixel_rgn->w);
	  for (x = 0; x < pixel_rgn->w; x++)
	    if (!try_fwrite (buffer + x*pixel_rgn->bpp + 3, 1, 1, file))
	      {
		g_free (buffer);
		return FALSE;
	      }
#if 0
	  if (width > pixel_rgn->w)
	    {
	      memset (buffer, 0, width - pixel_rgn->w);
	      if (!try_fwrite (buffer, width - pixel_rgn->w, 1, file))
		{
		  g_free (buffer);
		  return FALSE;
		}
	    }
#endif
	}
      if (y % 10 == 0)
	gimp_progress_update
	  (((double) index + 0.5 * y / pixel_rgn->h) / total);
    }

  gimp_progress_update (((double) index + 0.5) / total);

  patternheader.header_size = g_htonl (sizeof (patternheader) + strlen (DUMMY_PATTERN_NAME) + 1 );
  patternheader.version = g_htonl (1);
  patternheader.width = g_htonl (width);
  patternheader.height = g_htonl (height);
  patternheader.bytes = g_htonl (3);
  patternheader.magic_number = g_htonl (GPATTERN_MAGIC);

  if (!try_fwrite (&patternheader, sizeof (patternheader), 1, file))
    return FALSE;

  /* Pattern name is irrelevant */
  if (!try_fwrite (DUMMY_PATTERN_NAME, strlen (DUMMY_PATTERN_NAME) + 1, 1, file))
    return FALSE;

  for (y = 0; y < height; y++)
    {
#if 0
      if (y >= pixel_rgn->h)
	{
	  if (y == pixel_rgn->h)
	    memset (buffer, 0, width * 3);
	  if (!try_fwrite (buffer, width*3, 1, file))
	    {
	      g_free (buffer);
	      return FALSE;
	    }
	}
      else
#endif
	{
	  gimp_pixel_rgn_get_row (pixel_rgn, buffer,
				  0 + pixel_rgn->x, y + pixel_rgn->y, pixel_rgn->w);
	  for (x = 0; x < pixel_rgn->w; x++)
	    if (!try_fwrite (buffer + x*pixel_rgn->bpp, 3, 1, file))
	      return FALSE;
#if 0
	  if (width > pixel_rgn->w)
	    {
	      memset (buffer, 0, (width - pixel_rgn->w) * 3);
	      if (!try_fwrite (buffer, (width - pixel_rgn->w) * 3, 1, file))
		{
		  g_free (buffer);
		  return FALSE;
		}
	    }
#endif
	}
      
      if (y % 10 == 0)
	gimp_progress_update
	  (((double) index + 0.5 + 0.5 * y / pixel_rgn->h) / total);
    }
  g_free (buffer);

  gimp_progress_update (((double) index + 1.0) / total);

  return TRUE;
}

static gboolean
gpb_save_image (char   *filename,
		gint32  image_ID,
		gint32  drawable_ID)
{
904
  GDrawable *drawable;
Tor Lillqvist's avatar
Tor Lillqvist committed
905
  GPixelRgn pixel_rgn;
906
907
908
  FILE *file;
  gchar *temp;

909
910
911
912
913
  if (!(gimp_drawable_has_alpha (drawable_ID)))
    {
      g_warning ("drawable has no alpha channel -- aborting!\n");
      return (FALSE);
    }
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931

  drawable = gimp_drawable_get (drawable_ID);
  gimp_tile_cache_size (gimp_tile_height () * drawable->width * 4);

  gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0, drawable->width, drawable->height, FALSE, FALSE);

  temp = g_strdup_printf ("Saving %s:", filename);
  gimp_progress_init (temp);
  g_free (temp);

  file = fopen (filename, "wb");

  if (file == NULL)
    {
      g_message ("GPB: can't open \"%s\"", filename);
      return FALSE;
    }

Tor Lillqvist's avatar
Tor Lillqvist committed
932
  if (!save_one_gpb (file, &pixel_rgn, 0, 1))
933
934
    return FALSE;

Tor Lillqvist's avatar
Tor Lillqvist committed
935
936
937
938
939
940
941
942
  fclose (file);

  return TRUE;
}

static gboolean
gih_save_image (char   *filename,
		gint32  image_ID,
943
		gint32  orig_image_ID,
Tor Lillqvist's avatar
Tor Lillqvist committed
944
945
946
947
948
		gint32  drawable_ID)
{
  GDrawable *drawable;
  GPixelRgn pixel_rgn;
  FILE *file;
949
  Parasite *pipe_parasite;
950
  gchar *msg, *parstring, *ncells;
Tor Lillqvist's avatar
Tor Lillqvist committed
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
  gint32 *layer_ID;
  gint nlayers, layer, row, col;
  gint imagew, imageh, offsetx, offsety;
  gint k, x, y, thisx, thisy, xnext, ynext, thisw, thish;

  imagew = gimp_image_width (image_ID);
  imageh = gimp_image_height (image_ID);
  gimp_tile_cache_size (gimp_tile_height () * imagew * 4);

  msg = g_strdup_printf ("Saving %s:", filename);
  gimp_progress_init (msg);
  g_free (msg);

  file = fopen (filename, "wb");

  if (file == NULL)
    {
      g_message ("GPB: can't open \"%s\"", filename);
      return FALSE;
    }

972
973
  parstring = pixpipeparams_build (&gihparms);
  IFDBG(2) g_message ("parameter string: %s", parstring);
Tor Lillqvist's avatar
Tor Lillqvist committed
974
975
976
977
  ncells = g_strdup_printf ("%d ", gihparms.ncells);
  if (!(try_fwrite (info.description, strlen (info.description), 1, file)
	&& try_fwrite ("\n", 1, 1, file)
	&& try_fwrite (ncells, strlen (ncells), 1, file)
978
	&& try_fwrite (parstring, strlen (parstring), 1, file)
Tor Lillqvist's avatar
Tor Lillqvist committed
979
	&& try_fwrite ("\n", 1, 1, file)))
980
    {
981
      g_free (parstring);
Tor Lillqvist's avatar
Tor Lillqvist committed
982
983
      g_free (ncells);
      return FALSE;
984
    }
985
986
987

  pipe_parasite = parasite_new ("gimp-brush-pipe-parameters",
				PARASITE_PERSISTENT,
988
				strlen (parstring) + 1, parstring);
Marc Lehmann's avatar
Marc Lehmann committed
989
  gimp_image_parasite_attach (orig_image_ID, pipe_parasite);
990
991
  parasite_free (pipe_parasite);

992
  g_free (parstring);
Tor Lillqvist's avatar
Tor Lillqvist committed
993
994
995
996
997
998
999
1000
1001
1002
1003
  g_free (ncells);

  layer_ID = gimp_image_get_layers (image_ID, &nlayers);
  
  k = 0;
  for (layer = 0; layer < nlayers; layer++)
    {
      if (!gimp_drawable_has_alpha (layer_ID[layer]))
	continue;
      drawable = gimp_drawable_get (layer_ID[layer]);
      gimp_drawable_offsets(layer_ID[layer], &offsetx, &offsety);
1004

Tor Lillqvist's avatar
Tor Lillqvist committed
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
      for (row = 0; row < gihparms.rows; row++)
	{
	  y = (row * imageh) / gihparms.rows ;
	  ynext = ((row + 1) * imageh / gihparms.rows);
	  /* Assume layer is offset to positive direction in x and y.
	   * That's reasonable, as otherwise all of the layer
	   * won't be visible.
	   * thisy and thisx are in the drawable's coordinate space.
	   */
	  thisy = MAX (0, y - offsety);
	  thish = (ynext - offsety) - thisy;
	  thish = MIN (thish, drawable->height - thisy);
	  for (col = 0; col < gihparms.cols; col++)
	    {
	      x = (col * imagew / gihparms.cols);
	      xnext = ((col + 1) * imagew / gihparms.cols);
	      thisx = MAX (0, x - offsetx);
	      thisw = (xnext - offsetx) - thisx;
	      thisw = MIN (thisw, drawable->width - thisx);
	      IFDBG(3) g_message ("gimp_pixel_rgn_init %d %d %d %d",
				  thisx, thisy, thisw, thish);
	      gimp_pixel_rgn_init (&pixel_rgn, drawable, thisx, thisy, 
				   thisw, thish, FALSE, FALSE);
	      if (!save_one_gpb (file, &pixel_rgn, k, gihparms.ncells))
		return FALSE;
	      k++;
	    }
	}

    }
  
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
  gimp_progress_update (1.0);

  fclose (file);

  return TRUE;
}

static void
run (char    *name,
     int      nparams,
     GParam  *param,
     int     *nreturn_vals,
     GParam **return_vals)
{
  static GParam values[1];
  GRunModeType run_mode;
  GStatusType status = STATUS_SUCCESS;
Tor Lillqvist's avatar
Tor Lillqvist committed
1053
  Parasite *pipe_parasite;
1054
1055
1056
  gint32 image_ID;
  gint32 orig_image_ID;
  gint32 drawable_ID, *layer_ID;
Tor Lillqvist's avatar
Tor Lillqvist committed
1057
1058
  gint nlayers, layer;
  gchar *layer_name;
1059
  GimpExportReturnType export = EXPORT_CANCEL;
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069

  run_mode = param[0].data.d_int32;

  *return_vals = values;
  *nreturn_vals = 1;
  values[0].type = PARAM_STATUS;
  values[0].data.d_status = STATUS_CALLING_ERROR;

  if (strcmp (name, "file_gpb_save") == 0)
    {
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
      image_ID    = param[1].data.d_int32;
      drawable_ID = param[2].data.d_int32;
      
      /*  eventually export the image */ 
      switch (run_mode)
	{
	case RUN_INTERACTIVE:
	case RUN_WITH_LAST_VALS:
	  init_gtk ();
	  export = gimp_export_image (&image_ID, &drawable_ID, "GPB", 
1080
1081
				      (CAN_HANDLE_RGB | CAN_HANDLE_ALPHA | 
				       NEEDS_ALPHA ));
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
	  if (export == EXPORT_CANCEL)
	    {
	      *nreturn_vals = 1;
	      values[0].data.d_status = STATUS_EXECUTION_ERROR;
	      return;
	    }
	  break;
	default:
	  break;
	}

      switch (run_mode) 
	{
	case RUN_INTERACTIVE:
	  /*  Possibly retrieve data  */
	  gimp_get_data ("file_gpb_save", &info);
	  if (!gpb_save_dialog ())
	    return;
	  break;
	  
	case RUN_NONINTERACTIVE:  /* FIXME - need a real RUN_NONINTERACTIVE */
	  if (nparams != 7)
	    status = STATUS_CALLING_ERROR;
	  if (status == STATUS_SUCCESS)
	    {
	      info.spacing = param[5].data.d_int32;
	      strncpy (info.description, param[6].data.d_string, MAXDESCLEN);
	      info.description[MAXDESCLEN] = 0;
	    }
	  break;
	  
	case RUN_WITH_LAST_VALS:
	  gimp_get_data ("file_gpb_save", &info);
	  break;
	}
      
      if (gpb_save_image (param[3].data.d_string, image_ID, drawable_ID))
Tor Lillqvist's avatar
Tor Lillqvist committed
1119
1120
1121
1122
	{
	  gimp_set_data ("file_gpb_save", &info, sizeof (info));
	  status = STATUS_SUCCESS;
	}
1123
1124
      else
	status = STATUS_EXECUTION_ERROR;
1125
1126
1127

      if (export == EXPORT_EXPORT)
	gimp_image_delete (image_ID);
1128
    }
Tor Lillqvist's avatar
Tor Lillqvist committed
1129
1130
  else if (strcmp (name, "file_gih_save") == 0)
    {
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
      image_ID = orig_image_ID = param[1].data.d_int32;
      drawable_ID = param[2].data.d_int32; 

      /*  eventually export the image */ 
      switch (run_mode)
	{
	case RUN_INTERACTIVE:
	case RUN_WITH_LAST_VALS:
	  init_gtk ();
	  export = gimp_export_image (&image_ID, &drawable_ID, "GIH", 
1141
1142
				      (CAN_HANDLE_RGB | CAN_HANDLE_ALPHA | 
				       CAN_HANDLE_LAYERS | NEEDS_ALPHA));
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
	  if (export == EXPORT_CANCEL)
	    {
	      *nreturn_vals = 1;
	      values[0].data.d_status = STATUS_EXECUTION_ERROR;
	      return;
	    }
	  break;
	default:
	  break;
	}

Tor Lillqvist's avatar
Tor Lillqvist committed
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
      layer_ID = gimp_image_get_layers (image_ID, &nlayers);
      num_layers_with_alpha = 0;
      for (layer = 0; layer < nlayers; layer++)
	{
	  if (!gimp_drawable_has_alpha (layer_ID[layer]))
	    {
	      layer_name = gimp_layer_get_name (layer_ID[layer]);
	      g_message ("Layer %s doesn't have an alpha channel, skipped",
			 layer_name);
	      g_free (layer_name);
	    }
	  num_layers_with_alpha++;
	}
      
      IFDBG(2) g_message ("nlayers:%d num_layers_with_alpha:%d",
			  nlayers, num_layers_with_alpha);
      switch (run_mode)
	{
	case RUN_INTERACTIVE:
	  /*  Possibly retrieve data  */
	  gimp_get_data ("file_gih_save", &info);
Marc Lehmann's avatar
Marc Lehmann committed
1175
	  pipe_parasite = gimp_image_parasite_find (orig_image_ID, "gimp-brush-pipe-parameters");
1176
	  pixpipeparams_init (&gihparms);
Tor Lillqvist's avatar
Tor Lillqvist committed
1177
	  if (pipe_parasite)
1178
	    pixpipeparams_parse (pipe_parasite->data, &gihparms);
1179

1180
	  if (!gih_save_dialog (image_ID))
Tor Lillqvist's avatar
Tor Lillqvist committed
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
	    return;
	  break;

	case RUN_NONINTERACTIVE:  /* FIXME - need a real RUN_NONINTERACTIVE */
	  if (nparams != 7)
	    status = STATUS_CALLING_ERROR;
	  if (status == STATUS_SUCCESS)
	    {
	      info.spacing = param[5].data.d_int32;
	      strncpy (info.description, param[6].data.d_string, MAXDESCLEN);
	      info.description[MAXDESCLEN] = 0;
	    }
	  break;
	  
	case RUN_WITH_LAST_VALS:
	  gimp_get_data ("file_gih_save", &info);
Marc Lehmann's avatar
Marc Lehmann committed
1197
	  pipe_parasite = gimp_image_parasite_find (orig_image_ID, "gimp-brush-pipe-parameters");
1198
1199
1200
	  pixpipeparams_init (&gihparms);
	  if (pipe_parasite)
	    pixpipeparams_parse (pipe_parasite->data, &gihparms);
Tor Lillqvist's avatar
Tor Lillqvist committed
1201
1202
1203
	  break;
	}
      
1204
1205
      if (gih_save_image (param[3].data.d_string, 
			  image_ID, orig_image_ID, drawable_ID))
Tor Lillqvist's avatar
Tor Lillqvist committed
1206
1207
1208
1209
1210
1211
	{
	  gimp_set_data ("file_gih_save", &info, sizeof (info));
	  status = STATUS_SUCCESS;
	}
      else
	status = STATUS_EXECUTION_ERROR;
1212
1213
1214

        if (export == EXPORT_EXPORT)
	  gimp_image_delete (image_ID);
Tor Lillqvist's avatar
Tor Lillqvist committed
1215
    }
1216
1217
  values[0].data.d_status = status;
}