sunras.c 43.7 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1
2
3
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 * SUN raster reading and writing code Copyright (C) 1996 Peter Kirchgessner
4
 * (email: peter@kirchgessner.net, WWW: http://www.kirchgessner.net)
Elliot Lee's avatar
Elliot Lee committed
5
6
7
8
9
10
11
12
13
14
15
16
17
 *
 * 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
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
19
20
21
22
23
24
 *
 */

/* This program was written using pages 625-629 of the book
 * "Encyclopedia of Graphics File Formats", Murray/van Ryper,
 * O'Reilly & Associates Inc.
25
 * Bug reports or suggestions should be e-mailed to peter@kirchgessner.net
Elliot Lee's avatar
Elliot Lee committed
26
27
28
29
30
31
32
33
34
 */

/* Event history:
 * V 1.00, PK, 25-Jul-96: First try
 * V 1.90, PK, 15-Mar-97: Upgrade to work with GIMP V0.99
 * V 1.91, PK, 05-Apr-97: Return all arguments, even in case of an error
 * V 1.92, PK, 18-May-97: Ignore EOF-error on reading image data
 * V 1.93, PK, 05-Oct-97: Parse rc file
 * V 1.94, PK, 12-Oct-97: No progress bars for non-interactive mode
35
 * V 1.95, nn, 20-Dec-97: Initialize some variable
36
 * V 1.96, PK, 21-Nov-99: Internationalization
Elliot Lee's avatar
Elliot Lee committed
37
 */
38
static char ident[] = "@(#) GIMP SunRaster file-plugin v1.96  21-Nov-99";
Elliot Lee's avatar
Elliot Lee committed
39

40
41
#include "config.h"

Elliot Lee's avatar
Elliot Lee committed
42
43
44
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
45
#ifdef HAVE_UNISTD_H
46
#include <unistd.h>
47
48
#endif

Elliot Lee's avatar
Elliot Lee committed
49
50
#include "gtk/gtk.h"
#include "libgimp/gimp.h"
Sven Neumann's avatar
Sven Neumann committed
51
#include "libgimp/gimpui.h"
52
#include "libgimp/stdplugins-intl.h"
Elliot Lee's avatar
Elliot Lee committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

typedef int WRITE_FUN(void*,size_t,size_t,FILE*);

typedef unsigned long L_CARD32;
typedef unsigned short L_CARD16;
typedef unsigned char L_CARD8;

/* Fileheader of SunRaster files */
typedef struct {
  L_CARD32 l_ras_magic;    /* Magic Number */
  L_CARD32 l_ras_width;    /* Width */
  L_CARD32 l_ras_height;   /* Height */
  L_CARD32 l_ras_depth;    /* Number of bits per pixel (1,8,24,32) */
  L_CARD32 l_ras_length;   /* Length of image data (but may also be 0) */
  L_CARD32 l_ras_type;     /* Encoding */
  L_CARD32 l_ras_maptype;  /* Type of colormap */
  L_CARD32 l_ras_maplength;/* Number of bytes for colormap */
} L_SUNFILEHEADER;

/* Sun-raster magic */
#define RAS_MAGIC 0x59a66a95

#define RAS_TYPE_STD 1    /* Standard uncompressed format */
#define RAS_TYPE_RLE 2    /* Runlength compression format */

typedef struct {
 int val;   /* The value that is to be repeated */
 int n;     /* How many times it is repeated */
} RLEBUF;


/* Declare some local functions.
 */
static void   query      (void);
static void   run        (char    *name,
                          int      nparams,
                          GParam  *param,
                          int     *nreturn_vals,
                          GParam **return_vals);

Sven Neumann's avatar
Sven Neumann committed
93
94
95
96
static gint32 load_image (char    *filename);
static gint   save_image (char    *filename,
                          gint32   image_ID,
                          gint32   drawable_ID);
Elliot Lee's avatar
Elliot Lee committed
97
98
99
100

static void set_color_table (gint32, L_SUNFILEHEADER *, unsigned char *);

static gint32 create_new_image (char *filename, guint width, guint height,
Sven Neumann's avatar
Sven Neumann committed
101
102
103
104
105
106
107
				GImageType type, gint32 *layer_ID, GDrawable **drawable,
				GPixelRgn *pixel_rgn);

static gint32 load_sun_d1   (char *, FILE *, L_SUNFILEHEADER *, unsigned char *);
static gint32 load_sun_d8   (char *, FILE *, L_SUNFILEHEADER *, unsigned char *);
static gint32 load_sun_d24  (char *, FILE *, L_SUNFILEHEADER *, unsigned char *);
static gint32 load_sun_d32  (char *, FILE *, L_SUNFILEHEADER *, unsigned char *);
Elliot Lee's avatar
Elliot Lee committed
108
109
110

static L_CARD32 read_card32 (FILE *, int *);

Sven Neumann's avatar
Sven Neumann committed
111
static void write_card32    (FILE *, L_CARD32);
Elliot Lee's avatar
Elliot Lee committed
112

Sven Neumann's avatar
Sven Neumann committed
113
static void byte2bit        (unsigned char *, int, unsigned char *, int);
Elliot Lee's avatar
Elliot Lee committed
114

Sven Neumann's avatar
Sven Neumann committed
115
116
117
static void rle_startread   (FILE *);
static int  rle_fread       (char *, int, int, FILE *);
static int  rle_fgetc       (FILE *);
Elliot Lee's avatar
Elliot Lee committed
118
119
#define rle_getc(fp) ((rlebuf.n > 0) ? (rlebuf.n)--,rlebuf.val : rle_fgetc (fp))

Sven Neumann's avatar
Sven Neumann committed
120
121
122
123
124
static void rle_startwrite  (FILE *);
static int  rle_fwrite      (char *, int, int, FILE *);
static int  rle_fputc       (int, FILE *);
static int  rle_putrun      (int, int, FILE *);
static void rle_endwrite    (FILE *);
Elliot Lee's avatar
Elliot Lee committed
125
126
#define rle_putc rle_fputc

Sven Neumann's avatar
Sven Neumann committed
127
static void read_sun_header  (FILE *, L_SUNFILEHEADER *);
Elliot Lee's avatar
Elliot Lee committed
128
static void write_sun_header (FILE *, L_SUNFILEHEADER *);
Sven Neumann's avatar
Sven Neumann committed
129
130
static void read_sun_cols    (FILE *, L_SUNFILEHEADER *, unsigned char *);
static void write_sun_cols   (FILE *, L_SUNFILEHEADER *, unsigned char *);
Elliot Lee's avatar
Elliot Lee committed
131

Sven Neumann's avatar
Sven Neumann committed
132
133
static gint save_index       (FILE *, gint32, gint32 , int, int);
static gint save_rgb         (FILE *, gint32, gint32, int);
Elliot Lee's avatar
Elliot Lee committed
134
135
136
137
138

static int read_msb_first = 1;
static RLEBUF rlebuf;

/* Dialog-handling */
Sven Neumann's avatar
Sven Neumann committed
139
static void   init_gtk                 (void);
Elliot Lee's avatar
Elliot Lee committed
140
141
142
143
144
145
146
static gint   save_dialog              (void);
static void   save_close_callback      (GtkWidget *widget,
                                        gpointer   data);
static void   save_ok_callback         (GtkWidget *widget,
                                        gpointer   data);
static void   save_toggle_update       (GtkWidget *widget,
                                        gpointer   data);
Sven Neumann's avatar
Sven Neumann committed
147
static void   show_message             (char *);
Elliot Lee's avatar
Elliot Lee committed
148

Manish Singh's avatar
Manish Singh committed
149
150
151
/* Portability kludge */
static int my_fwrite (void *ptr, int size, int nmemb, FILE *stream);

Elliot Lee's avatar
Elliot Lee committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
GPlugInInfo PLUG_IN_INFO =
{
  NULL,    /* init_proc */
  NULL,    /* quit_proc */
  query,   /* query_proc */
  run,     /* run_proc */
};


/* Save info  */
typedef struct
{
  gint  rle;  /*  rle or standard */
} SUNRASSaveVals;

typedef struct
{
  gint  run;  /*  run  */
} SUNRASSaveInterface;


static SUNRASSaveVals psvals =
{
  TRUE     /* rle */
};

static SUNRASSaveInterface psint =
{
  FALSE     /* run */
};


/* The run mode */
static GRunModeType l_run_mode;

MAIN ()


static void
query (void)

{
  static GParamDef load_args[] =
  {
Sven Neumann's avatar
Sven Neumann committed
196
197
198
    { PARAM_INT32,  "run_mode",      "Interactive, non-interactive" },
    { PARAM_STRING, "filename",      "The name of the file to load" },
    { PARAM_STRING, "raw_filename",  "The name of the file to load" },
Elliot Lee's avatar
Elliot Lee committed
199
200
201
  };
  static GParamDef load_return_vals[] =
  {
Sven Neumann's avatar
Sven Neumann committed
202
    { PARAM_IMAGE,  "image",         "Output image" },
Elliot Lee's avatar
Elliot Lee committed
203
204
205
206
207
208
209
  };
  static int nload_args = sizeof (load_args) / sizeof (load_args[0]);
  static int nload_return_vals = sizeof (load_return_vals)
                               / sizeof (load_return_vals[0]);

  static GParamDef save_args[] =
  {
Sven Neumann's avatar
Sven Neumann committed
210
211
212
213
214
215
    { 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 image in" },
    { PARAM_STRING,   "raw_filename", "The name of the file to save the image in" },
    { PARAM_INT32,    "rle",          "Specify non-zero for rle output, zero for standard output" }
Elliot Lee's avatar
Elliot Lee committed
216
217
218
  };
  static int nsave_args = sizeof (save_args) / sizeof (save_args[0]);

219
220
  INIT_I18N();

Elliot Lee's avatar
Elliot Lee committed
221
  gimp_install_procedure ("file_sunras_load",
222
223
                          _("load file of the SunRaster file format"),
                          _("load file of the SunRaster file format"),
Elliot Lee's avatar
Elliot Lee committed
224
225
226
227
228
229
230
231
232
233
                          "Peter Kirchgessner",
                          "Peter Kirchgessner",
                          "1996",
                          "<Load>/SUNRAS",
                          NULL,
                          PROC_PLUG_IN,
                          nload_args, nload_return_vals,
                          load_args, load_return_vals);

  gimp_install_procedure ("file_sunras_save",
234
235
236
                          _("save file in the SunRaster file format"),
                          _("SUNRAS saving handles all image types except \
those with alpha channels."),
Elliot Lee's avatar
Elliot Lee committed
237
238
239
240
                          "Peter Kirchgessner",
                          "Peter Kirchgessner",
                          "1996",
                          "<Save>/SUNRAS",
Sven Neumann's avatar
Sven Neumann committed
241
                          "RGB, GRAY, INDEXED",
Elliot Lee's avatar
Elliot Lee committed
242
243
244
245
246
                          PROC_PLUG_IN,
                          nsave_args, 0,
                          save_args, NULL);

  /* Magic information for sunras would be "0,long,0x59a66a95" */
Sven Neumann's avatar
Sven Neumann committed
247
248
249
  gimp_register_magic_load_handler ("file_sunras_load", 
				    "im1,im8,im24,im32", 
				    "",
Elliot Lee's avatar
Elliot Lee committed
250
				    "0,long,0x59a66a95");
Sven Neumann's avatar
Sven Neumann committed
251
252
253
  gimp_register_save_handler       ("file_sunras_save", 
				    "im1,im8,im24,im32", 
				    "");
Elliot Lee's avatar
Elliot Lee committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
}


static void
run (char    *name,
     int      nparams,
     GParam  *param,
     int     *nreturn_vals,
     GParam **return_vals)

{
  static GParam values[2];
  GRunModeType run_mode;
  GStatusType status = STATUS_SUCCESS;
  gint32 image_ID;
Sven Neumann's avatar
Sven Neumann committed
269
270
  gint32 drawable_ID;
  GimpExportReturnType export = EXPORT_CANCEL;
Elliot Lee's avatar
Elliot Lee committed
271
272
273
274
275
276
277
278
279
280

  l_run_mode = run_mode = param[0].data.d_int32;

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

  if (strcmp (name, "file_sunras_load") == 0)
    {
281
282
      INIT_I18N_UI();

Elliot Lee's avatar
Elliot Lee committed
283
284
285
286
287
288
289
290
291
292
      image_ID = load_image (param[1].data.d_string);

      *nreturn_vals = 2;
      status = (image_ID != -1) ? STATUS_SUCCESS : STATUS_EXECUTION_ERROR;
      values[0].data.d_status = status;
      values[1].type = PARAM_IMAGE;
      values[1].data.d_image = image_ID;
    }
  else if (strcmp (name, "file_sunras_save") == 0)
    {
293
294
      INIT_I18N_UI();

Sven Neumann's avatar
Sven Neumann committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
      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, "SUNRAS", 
				      (CAN_HANDLE_RGB | CAN_HANDLE_GRAY | CAN_HANDLE_INDEXED));
	  if (export == EXPORT_CANCEL)
	    {
	      *nreturn_vals = 1;
	      values[0].data.d_status = STATUS_EXECUTION_ERROR;
	      return;
	  }
	  break;
	default:
	  break;
	}
      
Elliot Lee's avatar
Elliot Lee committed
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
      switch (run_mode)
        {
        case RUN_INTERACTIVE:
          /*  Possibly retrieve data  */
          gimp_get_data ("file_sunras_save", &psvals);

          /*  First acquire information with a dialog  */
          if (! save_dialog ())
            return;
          break;

        case RUN_NONINTERACTIVE:
          /*  Make sure all the arguments are there!  */
          if (nparams != 6)
          {
            status = STATUS_CALLING_ERROR;
          }
          else
          {
            psvals.rle = (param[5].data.d_int32) ? TRUE : FALSE;
          }
          break;

        case RUN_WITH_LAST_VALS:
          /*  Possibly retrieve data  */
          gimp_get_data ("file_sunras_save", &psvals);
          break;

        default:
          break;
        }

      if (status == STATUS_SUCCESS)
      {
Sven Neumann's avatar
Sven Neumann committed
351
        if (save_image (param[3].data.d_string, image_ID, drawable_ID))
Elliot Lee's avatar
Elliot Lee committed
352
353
354
355
356
357
358
359
360
361
        {
          /*  Store psvals data  */
          gimp_set_data ("file_sunras_save", &psvals, sizeof (SUNRASSaveVals));
        }
        else
        {
          status = STATUS_EXECUTION_ERROR;
        }
      }
      values[0].data.d_status = status;
Sven Neumann's avatar
Sven Neumann committed
362
363
364

      if (export == EXPORT_EXPORT)
	gimp_image_delete (image_ID);
Elliot Lee's avatar
Elliot Lee committed
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
    }
}


static gint32
load_image (char *filename)

{
  gint32 image_ID;
  FILE *ifp;
  char *temp = ident; /* Just to satisfy gcc/lint */
  L_SUNFILEHEADER sunhdr;
  unsigned char *suncolmap = NULL;

  ifp = fopen (filename, "rb");
  if (!ifp)
  {
382
    show_message (_("cant open file for reading"));
Elliot Lee's avatar
Elliot Lee committed
383
384
385
386
387
388
389
390
    return (-1);
  }

  read_msb_first = 1;   /* SUN raster is always most significant byte first */

  read_sun_header (ifp, &sunhdr);
  if (sunhdr.l_ras_magic != RAS_MAGIC)
  {
391
    show_message(_("cant open file as SUN-raster-file"));
Elliot Lee's avatar
Elliot Lee committed
392
393
394
395
396
397
    fclose (ifp);
    return (-1);
  }

  if ((sunhdr.l_ras_type < 0) || (sunhdr.l_ras_type > 5))
  {
398
    show_message (_("the type of this SUN-rasterfile is not supported"));
Elliot Lee's avatar
Elliot Lee committed
399
400
401
402
403
404
405
406
407
408
    fclose (ifp);
    return (-1);
  }

  /* Is there a RGB colourmap ? */
  if ((sunhdr.l_ras_maptype == 1) && (sunhdr.l_ras_maplength > 0))
  {
    suncolmap = (unsigned char *)malloc (sunhdr.l_ras_maplength);
    if (suncolmap == NULL)
    {
Sven Neumann's avatar
Sven Neumann committed
409
      show_message ("cant get memory for colour map");
Elliot Lee's avatar
Elliot Lee committed
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
      fclose (ifp);
      return (-1);
    }

    read_sun_cols (ifp, &sunhdr, suncolmap);
#ifdef DEBUG
    {int j, ncols;
     printf ("File %s\n",filename);
     ncols = sunhdr.l_ras_maplength/3;
     for (j=0; j < ncols; j++)
       printf ("Entry 0x%08x: 0x%04x,  0x%04x, 0x%04x\n",
               j,suncolmap[j],suncolmap[j+ncols],suncolmap[j+2*ncols]);
    }
#endif
    if (sunhdr.l_ras_magic != RAS_MAGIC)
    {
Sven Neumann's avatar
Sven Neumann committed
426
      show_message ("cant read colour entries");
Elliot Lee's avatar
Elliot Lee committed
427
428
429
430
431
432
      fclose (ifp);
      return (-1);
    }
  }
  else if (sunhdr.l_ras_maplength > 0)
  {
433
    show_message (_("type of colourmap not supported"));
Elliot Lee's avatar
Elliot Lee committed
434
435
436
437
438
439
    fseek (ifp, (sizeof (L_SUNFILEHEADER)/sizeof (L_CARD32))
               *4 + sunhdr.l_ras_maplength, SEEK_SET);
  }

  if (l_run_mode != RUN_NONINTERACTIVE)
  {
440
441
    temp = g_malloc (strlen (filename) + 64);
    sprintf (temp, _("Loading %s:"), filename);
Elliot Lee's avatar
Elliot Lee committed
442
443
444
445
446
447
448
449
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
    gimp_progress_init (temp);
    g_free (temp);
  }

  switch (sunhdr.l_ras_depth)
  {
    case 1:    /* bitmap */
      image_ID = load_sun_d1 (filename, ifp, &sunhdr, suncolmap);
      break;

    case 8:    /* 256 colours */
      image_ID = load_sun_d8 (filename, ifp, &sunhdr, suncolmap);
      break;

    case 24:   /* True color */
      image_ID = load_sun_d24 (filename, ifp, &sunhdr, suncolmap);
      break;

    case 32:   /* True color with extra byte */
      image_ID = load_sun_d32 (filename, ifp, &sunhdr, suncolmap);
      break;

    default:
     image_ID = -1;
     break;
  }

  fclose (ifp);

  if (suncolmap != NULL) free ((char *)suncolmap);

  if (image_ID == -1)
  {
475
    show_message (_("this image depth is not supported"));
Elliot Lee's avatar
Elliot Lee committed
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
    return (-1);
  }

  return (image_ID);
}


static gint
save_image (char *filename,
            gint32  image_ID,
            gint32  drawable_ID)

{
  FILE* ofp;
  GDrawableType drawable_type;
  gint retval;
  char *temp;

  drawable_type = gimp_drawable_type (drawable_ID);

  /*  Make sure we're not saving an image with an alpha channel  */
  if (gimp_drawable_has_alpha (drawable_ID))
  {
499
    show_message (_("SUNRAS save cannot handle images with alpha channels"));
Elliot Lee's avatar
Elliot Lee committed
500
501
502
503
504
505
506
507
508
509
    return FALSE;
  }

  switch (drawable_type)
  {
    case INDEXED_IMAGE:
    case GRAY_IMAGE:
    case RGB_IMAGE:
      break;
    default:
510
      show_message (_("cant operate on unknown image types"));
Elliot Lee's avatar
Elliot Lee committed
511
512
513
514
515
516
517
518
      return (FALSE);
      break;
  }

  /* Open the output file. */
  ofp = fopen (filename, "wb");
  if (!ofp)
  {
519
    show_message (_("cant open file for writing"));
Elliot Lee's avatar
Elliot Lee committed
520
521
522
523
524
    return (FALSE);
  }

  if (l_run_mode != RUN_NONINTERACTIVE)
  {
525
526
    temp = g_malloc (strlen (filename) + 64);
    sprintf (temp, _("Saving %s:"), filename);
Elliot Lee's avatar
Elliot Lee committed
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
    gimp_progress_init (temp);
    g_free (temp);
  }

  if (drawable_type == INDEXED_IMAGE)
    retval = save_index (ofp,image_ID, drawable_ID, 0, (int)psvals.rle);
  else if (drawable_type == GRAY_IMAGE)
    retval = save_index (ofp,image_ID, drawable_ID, 1, (int)psvals.rle);
  else if (drawable_type == RGB_IMAGE)
    retval = save_rgb (ofp,image_ID, drawable_ID, (int)psvals.rle);
  else
    retval = FALSE;

  fclose (ofp);

  return (retval);
}


static L_CARD32 read_card32 (FILE *ifp,
                             int *err)

{L_CARD32 c;

 if (read_msb_first)
 {
   c = (((L_CARD32)(getc (ifp))) << 24);
   c |= (((L_CARD32)(getc (ifp))) << 16);
   c |= (((L_CARD32)(getc (ifp))) << 8);
   c |= ((L_CARD32)(*err = getc (ifp)));
 }
 else
 {
   c = ((L_CARD32)(getc (ifp)));
   c |= (((L_CARD32)(getc (ifp))) << 8);
   c |= (((L_CARD32)(getc (ifp))) << 16);
   c |= (((L_CARD32)(*err = getc (ifp))) << 24);
 }

 *err = (*err < 0);
 return (c);
}


Sven Neumann's avatar
Sven Neumann committed
571
572
573
static void 
write_card32 (FILE     *ofp,
	      L_CARD32  c)
Elliot Lee's avatar
Elliot Lee committed
574
575
576
577
578
579
580
581
582
583

{
 putc ((int)((c >> 24) & 0xff), ofp);
 putc ((int)((c >> 16) & 0xff), ofp);
 putc ((int)((c >> 8) & 0xff), ofp);
 putc ((int)((c) & 0xff), ofp);
}


/* Convert n bytes of 0/1 to a line of bits */
Sven Neumann's avatar
Sven Neumann committed
584
585
586
587
588
static void 
byte2bit (unsigned char *byteline,
	  int            width,
	  unsigned char *bitline,
	  int            invert)
Elliot Lee's avatar
Elliot Lee committed
589

Sven Neumann's avatar
Sven Neumann committed
590
591
592
{
  register unsigned char bitval;
  unsigned char rest[8];
Elliot Lee's avatar
Elliot Lee committed
593

Sven Neumann's avatar
Sven Neumann committed
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
  while (width >= 8)
    {
      bitval = 0;
      if (*(byteline++)) bitval |= 0x80;
      if (*(byteline++)) bitval |= 0x40;
      if (*(byteline++)) bitval |= 0x20;
      if (*(byteline++)) bitval |= 0x10;
      if (*(byteline++)) bitval |= 0x08;
      if (*(byteline++)) bitval |= 0x04;
      if (*(byteline++)) bitval |= 0x02;
      if (*(byteline++)) bitval |= 0x01;
      *(bitline++) = invert ? ~bitval : bitval;
      width -= 8;
    }
  if (width > 0)
    {
      memset (rest, 0, 8);
      memcpy (rest, byteline, width);
      bitval = 0;
      byteline = rest;
      if (*(byteline++)) bitval |= 0x80;
      if (*(byteline++)) bitval |= 0x40;
      if (*(byteline++)) bitval |= 0x20;
      if (*(byteline++)) bitval |= 0x10;
      if (*(byteline++)) bitval |= 0x08;
      if (*(byteline++)) bitval |= 0x04;
      if (*(byteline++)) bitval |= 0x02;
      *bitline = invert ? ~bitval : bitval;
    }
Elliot Lee's avatar
Elliot Lee committed
623
624
625
626
}


/* Start reading Runlength Encoded Data */
Sven Neumann's avatar
Sven Neumann committed
627
628
629
static void 
rle_startread (FILE *ifp)
     
Elliot Lee's avatar
Elliot Lee committed
630
631
632
633
634
635
{ /* Clear RLE-buffer */
  rlebuf.val = rlebuf.n = 0;
}


/* Read uncompressed elements from RLE-stream */
Sven Neumann's avatar
Sven Neumann committed
636
637
638
639
640
641
642
643
static int 
rle_fread (char *ptr,
	   int   sz,
	   int   nelem,
	   FILE *ifp)
     
{
  int elem_read, cnt, val, err = 0;
Elliot Lee's avatar
Elliot Lee committed
644

Sven Neumann's avatar
Sven Neumann committed
645
646
647
648
649
650
651
652
653
654
655
  for (elem_read = 0; elem_read < nelem; elem_read++)
    {
      for (cnt = 0; cnt < sz; cnt++)
	{
	  val = rle_getc (ifp);
	  if (val < 0) { err = 1; break; }
	  *(ptr++) = (char)val;
	}
      if (err) break;
    }
  return (elem_read);
Elliot Lee's avatar
Elliot Lee committed
656
657
658
659
}


/* Get one byte of uncompressed data from RLE-stream */
Sven Neumann's avatar
Sven Neumann committed
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
static int 
rle_fgetc (FILE *ifp)
{
  int flag, runcnt, runval;
  
  if (rlebuf.n > 0)    /* Something in the buffer ? */
    {
      (rlebuf.n)--;
      return (rlebuf.val);
    }
  
  /* Nothing in the buffer. We have to read something */
  if ((flag = getc (ifp)) < 0) return (-1);
  if (flag != 0x0080) return (flag);    /* Single byte run ? */
  
  if ((runcnt = getc (ifp)) < 0) return (-1);
  if (runcnt == 0) return (0x0080);     /* Single 0x80 ? */
  
  /* The run */
  if ((runval = getc (ifp)) < 0) return (-1);
  rlebuf.n = runcnt;
  rlebuf.val = runval;
  return (runval);
Elliot Lee's avatar
Elliot Lee committed
683
684
685
686
}


/* Start writing Runlength Encoded Data */
Sven Neumann's avatar
Sven Neumann committed
687
688
static void 
rle_startwrite (FILE *ofp)
Elliot Lee's avatar
Elliot Lee committed
689
690
691
692
693
694
{ /* Clear RLE-buffer */
  rlebuf.val = rlebuf.n = 0;
}


/* Write uncompressed elements to RLE-stream */
Sven Neumann's avatar
Sven Neumann committed
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
static int 
rle_fwrite (char *ptr,
	    int   sz,
	    int   nelem,
	    FILE *ofp)
     
{
  int elem_write, cnt, val, err = 0;
  unsigned char *pixels = (unsigned char *)ptr;
  
  for (elem_write = 0; elem_write < nelem; elem_write++)
    {
      for (cnt = 0; cnt < sz; cnt++)
	{
	  val = rle_fputc (*(pixels++), ofp);
	  if (val < 0) { err = 1; break; }
	}
      if (err) break;
    }
  return (elem_write);
Elliot Lee's avatar
Elliot Lee committed
715
716
717
718
}


/* Write uncompressed character to RLE-stream */
Sven Neumann's avatar
Sven Neumann committed
719
720
721
static int 
rle_fputc (int   val,
	   FILE *ofp)
Elliot Lee's avatar
Elliot Lee committed
722

Sven Neumann's avatar
Sven Neumann committed
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
{
  int retval;
  
  if (rlebuf.n == 0)    /* Nothing in the buffer ? Save the value */
    {
      rlebuf.n = 1;
      return (rlebuf.val = val);
    }
  
  /* Something in the buffer */
  
  if (rlebuf.val == val)   /* Same value in the buffer ? */
    {
      (rlebuf.n)++;
      if (rlebuf.n == 257) /* Can not be encoded in a single run ? */
	{
	  retval = rle_putrun (256, rlebuf.val, ofp);
	  if (retval < 0) return (retval);
	  rlebuf.n -= 256;
	}
      return (val);
    }
  
  /* Something different in the buffer ? Write out the run */
  
  retval = rle_putrun (rlebuf.n, rlebuf.val, ofp);
  if (retval < 0) return (retval);
  
  /* Save the new value */
  rlebuf.n = 1;
  return (rlebuf.val = val);
Elliot Lee's avatar
Elliot Lee committed
754
755
756
757
}


/* Write out a run with 0 < n < 257 */
Sven Neumann's avatar
Sven Neumann committed
758
759
760
761
static int 
rle_putrun (int   n,
	    int   val,
	    FILE *ofp)
Elliot Lee's avatar
Elliot Lee committed
762

Sven Neumann's avatar
Sven Neumann committed
763
764
{
  int retval, flag = 0x80;
Elliot Lee's avatar
Elliot Lee committed
765

Sven Neumann's avatar
Sven Neumann committed
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
  /* Useful to write a 3 byte run ? */
  if ((n > 2) || ((n == 2) && (val == flag)))
    {
      putc (flag, ofp);
      putc (n-1, ofp);
      retval = putc (val, ofp);
    }
  else if (n == 2) /* Write two single runs (could not be value 0x80) */
    {
      putc (val, ofp);
      retval = putc (val, ofp);
    }
  else  /* Write a single run */
    {
      if (val == flag)
	retval = putc (flag, ofp), putc (0x00, ofp);
      else
	retval = putc (val, ofp);
    }
  
  return ((retval < 0) ? retval : val);
Elliot Lee's avatar
Elliot Lee committed
787
788
789
790
}


/* End writing Runlength Encoded Data */
Sven Neumann's avatar
Sven Neumann committed
791
792
static void 
rle_endwrite (FILE *ofp)    
Elliot Lee's avatar
Elliot Lee committed
793
{
Sven Neumann's avatar
Sven Neumann committed
794
795
796
797
798
  if (rlebuf.n > 0)
    {
      rle_putrun (rlebuf.n, rlebuf.val, ofp);
      rlebuf.val = rlebuf.n = 0; /* Clear RLE-buffer */
    }
Elliot Lee's avatar
Elliot Lee committed
799
800
801
802
}


static void
Sven Neumann's avatar
Sven Neumann committed
803
read_sun_header (FILE            *ifp,
Elliot Lee's avatar
Elliot Lee committed
804
                 L_SUNFILEHEADER *sunhdr)
Sven Neumann's avatar
Sven Neumann committed
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
     
{
  int j, err;
  L_CARD32 *cp;
  
  cp = (L_CARD32 *)sunhdr;
  
  /* Read in all 32-bit values of the header and check for byte order */
  for (j = 0; j < sizeof (L_SUNFILEHEADER)/sizeof(sunhdr->l_ras_magic); j++)
    {
      *(cp++) = read_card32 (ifp, &err);
      if (err) break;
    }
  
  if (err) sunhdr->l_ras_magic = 0;  /* Not a valid SUN-raster file */
Elliot Lee's avatar
Elliot Lee committed
820
821
822
823
824
825
}


/* Write out a SUN-fileheader */

static void
Sven Neumann's avatar
Sven Neumann committed
826
write_sun_header (FILE            *ofp,
Elliot Lee's avatar
Elliot Lee committed
827
                  L_SUNFILEHEADER *sunhdr)
Sven Neumann's avatar
Sven Neumann committed
828
829
830
831
832
833
834
835
836
837
838
839
840
841
     
{
  int j, hdr_entries;
  L_CARD32 *cp;
  
  hdr_entries = sizeof (L_SUNFILEHEADER)/sizeof(sunhdr->l_ras_magic);
  
  cp = (L_CARD32 *)sunhdr;
  
  /* Write out all 32-bit values of the header and check for byte order */
  for (j = 0; j < sizeof (L_SUNFILEHEADER)/sizeof(sunhdr->l_ras_magic); j++)
    {
      write_card32 (ofp, *(cp++));
    }
Elliot Lee's avatar
Elliot Lee committed
842
843
844
845
846
847
}


/* Read the sun colourmap */

static void
Sven Neumann's avatar
Sven Neumann committed
848
read_sun_cols (FILE            *ifp,
Elliot Lee's avatar
Elliot Lee committed
849
               L_SUNFILEHEADER *sunhdr,
Sven Neumann's avatar
Sven Neumann committed
850
               unsigned char   *colormap)
Elliot Lee's avatar
Elliot Lee committed
851

Sven Neumann's avatar
Sven Neumann committed
852
853
854
855
856
857
858
859
860
861
862
{
  int ncols, err = 0;
  
  /* Read in SUN-raster Colormap */
  ncols = sunhdr->l_ras_maplength / 3;
  if (ncols <= 0)
    err = 1;
  else
    err = (fread (colormap, 3, ncols, ifp) != ncols);
  
  if (err) sunhdr->l_ras_magic = 0;  /* Not a valid SUN-raster file */
Elliot Lee's avatar
Elliot Lee committed
863
864
865
866
867
868
}


/* Write a sun colourmap */

static void
Sven Neumann's avatar
Sven Neumann committed
869
write_sun_cols (FILE            *ofp,
Elliot Lee's avatar
Elliot Lee committed
870
871
872
                L_SUNFILEHEADER *sunhdr,
                unsigned char *colormap)

Sven Neumann's avatar
Sven Neumann committed
873
874
875
876
877
{
  int ncols;
  
  ncols = sunhdr->l_ras_maplength / 3;
  fwrite (colormap, 3, ncols, ofp);
Elliot Lee's avatar
Elliot Lee committed
878
879
880
881
882
}


/* Set a GIMP colourtable using the sun colourmap */

Sven Neumann's avatar
Sven Neumann committed
883
static void set_color_table (gint32           image_ID,
Elliot Lee's avatar
Elliot Lee committed
884
                             L_SUNFILEHEADER *sunhdr,
Sven Neumann's avatar
Sven Neumann committed
885
                             guchar          *suncolmap)
Elliot Lee's avatar
Elliot Lee committed
886

Sven Neumann's avatar
Sven Neumann committed
887
888
889
890
891
892
893
894
895
896
897
898
899
900
{
  int ncols, j;
  unsigned char ColorMap[256*3];
  
  ncols = sunhdr->l_ras_maplength / 3;
  if (ncols <= 0) return;
  
  for (j = 0; j < ncols; j++)
    {
      ColorMap[j*3] = suncolmap[j];
      ColorMap[j*3+1] = suncolmap[j+ncols];
      ColorMap[j*3+2] = suncolmap[j+2*ncols];
    }
  
Elliot Lee's avatar
Elliot Lee committed
901
#ifdef DEBUG
Sven Neumann's avatar
Sven Neumann committed
902
903
904
905
  printf ("Set GIMP colortable:\n");
  for (j = 0; j < ncols; j++)
    printf ("%3d: 0x%02x 0x%02x 0x%02x\n", j,
	    ColorMap[j*3], ColorMap[j*3+1], ColorMap[j*3+2]);
Elliot Lee's avatar
Elliot Lee committed
906
#endif
Sven Neumann's avatar
Sven Neumann committed
907
  gimp_image_set_cmap (image_ID, ColorMap, ncols);
Elliot Lee's avatar
Elliot Lee committed
908
909
910
911
912
}


/* Create an image. Sets layer_ID, drawable and rgn. Returns image_ID */
static gint32
Sven Neumann's avatar
Sven Neumann committed
913
914
915
916
917
create_new_image (char       *filename,
                  guint       width,
                  guint       height,
                  GImageType  type,
                  gint32     *layer_ID,
Elliot Lee's avatar
Elliot Lee committed
918
                  GDrawable **drawable,
Sven Neumann's avatar
Sven Neumann committed
919
                  GPixelRgn  *pixel_rgn)
Elliot Lee's avatar
Elliot Lee committed
920

Sven Neumann's avatar
Sven Neumann committed
921
922
923
924
925
926
927
928
929
930
931
{
  gint32 image_ID;
  GDrawableType gdtype;
  
  if (type == GRAY) gdtype = GRAY_IMAGE;
  else if (type == INDEXED) gdtype = INDEXED_IMAGE;
  else gdtype = RGB_IMAGE;
  
  image_ID = gimp_image_new (width, height, type);
  gimp_image_set_filename (image_ID, filename);
  
932
  *layer_ID = gimp_layer_new (image_ID, _("Background"), width, height,
Elliot Lee's avatar
Elliot Lee committed
933
                            gdtype, 100, NORMAL_MODE);
Sven Neumann's avatar
Sven Neumann committed
934
935
936
937
938
939
940
  gimp_image_add_layer (image_ID, *layer_ID, 0);
  
  *drawable = gimp_drawable_get (*layer_ID);
  gimp_pixel_rgn_init (pixel_rgn, *drawable, 0, 0, (*drawable)->width,
		       (*drawable)->height, TRUE, FALSE);
  
  return (image_ID);
Elliot Lee's avatar
Elliot Lee committed
941
942
943
944
945
}


/* Load SUN-raster-file with depth 1 */
static gint32
Sven Neumann's avatar
Sven Neumann committed
946
947
load_sun_d1 (char            *filename,
             FILE            *ifp,
Elliot Lee's avatar
Elliot Lee committed
948
             L_SUNFILEHEADER *sunhdr,
Sven Neumann's avatar
Sven Neumann committed
949
             unsigned char   *suncolmap)
Elliot Lee's avatar
Elliot Lee committed
950

Sven Neumann's avatar
Sven Neumann committed
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
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
1036
1037
1038
1039
{
  register int pix8;
  int width, height, linepad, scan_lines, tile_height;
  int i, j;
  unsigned char *dest, *data;
  gint32 layer_ID, image_ID;
  GPixelRgn pixel_rgn;
  GDrawable *drawable;
  unsigned char bit2byte[256*8];
  L_SUNFILEHEADER sun_bwhdr;
  unsigned char sun_bwcolmap[6] = { 255,0,255,0,255,0 };
  int err = 0, rle = (sunhdr->l_ras_type == RAS_TYPE_RLE);
  
  width = sunhdr->l_ras_width;
  height = sunhdr->l_ras_height;
  
  image_ID = create_new_image (filename, width, height, INDEXED,
			       &layer_ID, &drawable, &pixel_rgn);
  
  tile_height = gimp_tile_height ();
  data = g_malloc (tile_height * width);
  
  if (suncolmap != NULL)   /* Set up the specified colour map */
    {
      set_color_table (image_ID, sunhdr, suncolmap);
    }
  else   /* No colourmap available. Set up a dummy b/w-colourmap */
    {      /* Copy the original header and simulate b/w-colourmap */
      memcpy ((char *)&sun_bwhdr,(char *)sunhdr,sizeof (L_SUNFILEHEADER));
      sun_bwhdr.l_ras_maptype = 2;
      sun_bwhdr.l_ras_maplength = 6;
      set_color_table (image_ID, &sun_bwhdr, sun_bwcolmap);
    }
  
  /* Get an array for mapping 8 bits in a byte to 8 bytes */
  dest = bit2byte;
  for (j = 0; j < 256; j++)
    for (i = 7; i >= 0; i--)
      *(dest++) = ((j & (1 << i)) != 0);
  
  linepad = (((sunhdr->l_ras_width+7)/8) % 2); /* Check for 16bit align */
  
  if (rle) rle_startread (ifp);
  
  dest = data;
  scan_lines = 0;
  
  for (i = 0; i < height; i++)
    {
      j = width;
      while (j >= 8)
	{
	  pix8 = rle ? rle_getc (ifp) : getc (ifp);
	  if (pix8 < 0) { err = 1; pix8 = 0; }
	  
	  memcpy (dest, bit2byte + pix8*8, 8);
	  dest += 8;
	  j -= 8;
	}
      
      if (j > 0)
	{
	  pix8 = rle ? rle_getc (ifp) : getc (ifp);
	  if (pix8 < 0) { err = 1; pix8 = 0; }
	  
	  memcpy (dest, bit2byte + pix8*8, j);
	  dest += j;
	}
      
      if (linepad)
	err |= ((rle ? rle_getc (ifp) : getc (ifp)) < 0);
      
      scan_lines++;
      
      if ((l_run_mode != RUN_NONINTERACTIVE) && ((i % 20) == 0))
	gimp_progress_update ((double)(i+1) / (double)height);
      
      if ((scan_lines == tile_height) || ((i+1) == height))
	{
	  gimp_pixel_rgn_set_rect (&pixel_rgn, data, 0, i-scan_lines+1,
				   width, scan_lines);
	  scan_lines = 0;
	  dest = data;
	}
    }
  
  g_free (data);
  
  if (err)
1040
    show_message (_("EOF encountered on reading"));
Sven Neumann's avatar
Sven Neumann committed
1041
1042
1043
1044
  
  gimp_drawable_flush (drawable);
  
  return (image_ID);
Elliot Lee's avatar
Elliot Lee committed
1045
1046
1047
1048
1049
1050
}


/* Load SUN-raster-file with depth 8 */

static gint32
Sven Neumann's avatar
Sven Neumann committed
1051
1052
load_sun_d8 (char            *filename,
             FILE            *ifp,
Elliot Lee's avatar
Elliot Lee committed
1053
             L_SUNFILEHEADER *sunhdr,
Sven Neumann's avatar
Sven Neumann committed
1054
             unsigned char   *suncolmap)
Elliot Lee's avatar
Elliot Lee committed
1055

Sven Neumann's avatar
Sven Neumann committed
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
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
{
  int width, height, linepad, i, j;
  int greyscale, ncols;
  int scan_lines, tile_height;
  unsigned char *dest, *data;
  gint32 layer_ID, image_ID;
  GPixelRgn pixel_rgn;
  GDrawable *drawable;
  int err = 0, rle = (sunhdr->l_ras_type == RAS_TYPE_RLE);
  
  width = sunhdr->l_ras_width;
  height = sunhdr->l_ras_height;
  
  /* This could also be a greyscale image. Check it */
  ncols = sunhdr->l_ras_maplength / 3;
  
  greyscale = 1;  /* Also greyscale if no colourmap present */
  
  if ((ncols > 0) && (suncolmap != NULL))
    {
      for (j = 0; j < ncols; j++)
	{
	  if (   (suncolmap[j] != j)
		 || (suncolmap[j+ncols] != j)
		 || (suncolmap[j+2*ncols] != j))
	    {
	      greyscale = 0;
	      break;
	    }
	}
    }
  
  image_ID = create_new_image (filename, width, height,
			       greyscale ? GRAY : INDEXED,
			       &layer_ID, &drawable, &pixel_rgn);
  
  tile_height = gimp_tile_height ();
  data = g_malloc (tile_height * width);
  
  if (!greyscale)
    set_color_table (image_ID, sunhdr, suncolmap);
  
  linepad = (sunhdr->l_ras_width % 2);
  
  if (rle) rle_startread (ifp);  /* Initialize RLE-buffer */
  
  dest = data;
  scan_lines = 0;
  
  for (i = 0; i < height; i++)
    {
      memset ((char *)dest, 0, width);
      err |= ((rle ? rle_fread ((char *)dest, 1, width, ifp)
Elliot Lee's avatar
Elliot Lee committed
1109
               : fread ((char *)dest, 1, width, ifp)) != width);
Sven Neumann's avatar
Sven Neumann committed
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
      
      if (linepad)
	err |= ((rle ? rle_getc (ifp) : getc (ifp)) < 0);
      
      dest += width;
      scan_lines++;
      
      if ((l_run_mode != RUN_NONINTERACTIVE) && ((i % 20) == 0))
	gimp_progress_update ((double)(i+1) / (double)height);
      
      if ((scan_lines == tile_height) || ((i+1) == height))
	{
	  gimp_pixel_rgn_set_rect (&pixel_rgn, data, 0, i-scan_lines+1,
				   width, scan_lines);
	  scan_lines = 0;
	  dest = data;
	}
    }
  
  g_free (data);
  
  if (err)
1132
    show_message (_("EOF encountered on reading"));
Sven Neumann's avatar
Sven Neumann committed
1133
1134
1135
1136
  
  gimp_drawable_flush (drawable);
  
  return (image_ID);
Elliot Lee's avatar
Elliot Lee committed
1137
1138
1139
1140
1141
}


/* Load SUN-raster-file with depth 24 */
static gint32
Sven Neumann's avatar
Sven Neumann committed
1142
1143
1144
1145
load_sun_d24 (char             *filename,
              FILE             *ifp,
              L_SUNFILEHEADER  *sunhdr,
              unsigned char    *suncolmap)
Elliot Lee's avatar
Elliot Lee committed
1146

Sven Neumann's avatar
Sven Neumann committed
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
{
  register unsigned char *dest, blue;
  unsigned char *data;
  int width, height, linepad, tile_height, scan_lines;
  int i, j;
  gint32 layer_ID, image_ID;
  GPixelRgn pixel_rgn;
  GDrawable *drawable;
  int err = 0, rle = (sunhdr->l_ras_type == RAS_TYPE_RLE);
  
  width = sunhdr->l_ras_width;
  height = sunhdr->l_ras_height;
  
  image_ID = create_new_image (filename, width, height, RGB,
			       &layer_ID, &drawable, &pixel_rgn);
  
  tile_height = gimp_tile_height ();
  data = g_malloc (tile_height * width * 3);
  
  linepad = ((sunhdr->l_ras_width*3) % 2);

  if (rle) rle_startread (ifp);  /* Initialize RLE-buffer */
  
  dest = data;
  scan_lines = 0;
  
  for (i = 0; i < height; i++)
    {
      memset ((char *)dest, 0, 3*width);
      err |= ((rle ? rle_fread ((char *)dest, 3, width, ifp)
Elliot Lee's avatar
Elliot Lee committed
1177
               : fread ((char *)dest, 3, width, ifp)) != width);
Sven Neumann's avatar
Sven Neumann committed
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
      
      if (linepad)
	err |= ((rle ? rle_getc (ifp) : getc (ifp)) < 0);
      
      if (sunhdr->l_ras_type == 3) /* RGB-format ? That is what GIMP wants */
	{
	  dest += width*3;
	}
      else                         /* We have BGR format. Correct it */
	{
	  for (j = 0; j < width; j++)
	    {
	      blue = *dest;
	      *dest = *(dest+2);
	      *(dest+2) = blue;
	      dest += 3;
	    }
	}
      
      scan_lines++;
      
      if ((l_run_mode != RUN_NONINTERACTIVE) && ((i % 20) == 0))
	gimp_progress_update ((double)(i+1) / (double)height);
      
      if ((scan_lines == tile_height) || ((i+1) == height))
	{
	  gimp_pixel_rgn_set_rect (&pixel_rgn, data, 0, i-scan_lines+1,
				   width, scan_lines);
	  scan_lines = 0;
	  dest = data;
	}
    }
  
  g_free (data);
  
  if (err)
1214
    show_message (_("EOF encountered on reading"));
Sven Neumann's avatar
Sven Neumann committed
1215
1216
1217
1218
  
  gimp_drawable_flush (drawable);
  
  return (image_ID);
Elliot Lee's avatar
Elliot Lee committed
1219
1220
1221
1222
1223
1224
}


/* Load SUN-raster-file with depth 32 */

static gint32
Sven Neumann's avatar
Sven Neumann committed
1225
1226
load_sun_d32 (char            *filename,
              FILE            *ifp,
Elliot Lee's avatar
Elliot Lee committed
1227
              L_SUNFILEHEADER *sunhdr,
Sven Neumann's avatar
Sven Neumann committed
1228
              unsigned char   *suncolmap)
Elliot Lee's avatar
Elliot Lee committed
1229

Sven Neumann's avatar
Sven Neumann committed
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
{
  register unsigned char *dest, blue;
  unsigned char *data;
  int width, height, tile_height, scan_lines;
  int i, j;
  gint32 layer_ID, image_ID;
  GPixelRgn pixel_rgn;
  GDrawable *drawable;
  int err = 0, cerr, rle = (sunhdr->l_ras_type == RAS_TYPE_RLE);
  width = sunhdr->l_ras_width;
  height = sunhdr->l_ras_height;
  
  /* initialize */
  
  cerr = 0;
  
  image_ID = create_new_image (filename, width, height, RGB,
			       &layer_ID, &drawable, &pixel_rgn);
  
  tile_height = gimp_tile_height ();
  data = g_malloc (tile_height * width * 3);
  
  if (rle) rle_startread (ifp);  /* Initialize RLE-buffer */
  
  dest = data;
  scan_lines = 0;
  
  for (i = 0; i < height; i++)
    {
      if (rle)
	{
	  for (j = 0; j < width; j++)
	    {
	      rle_getc (ifp);   /* Skip unused byte */
	      *(dest++) = rle_getc (ifp);
	      *(dest++) = rle_getc (ifp);
	      *(dest++) = (cerr = (rle_getc (ifp)));
	    }
	}
      else
	{
	  for (j = 0; j < width; j++)
	    {
	      getc (ifp);   /* Skip unused byte */
	      *(dest++) = getc (ifp);
	      *(dest++) = getc (ifp);
	      *(dest++) = (cerr = (getc (ifp)));
	    }
	}
      err |= (cerr < 0);
      
      if (sunhdr->l_ras_type != 3) /* BGR format ? Correct it */
	{
	  for (j = 0; j < width; j++)
	    {
	      dest -= 3;
	      blue = *dest;
	      *dest = *(dest+2);
	      *(dest+2) = blue;
	    }
	  dest += width*3;
	}
      
      scan_lines++;
      
      if ((l_run_mode != RUN_NONINTERACTIVE) && ((i % 20) == 0))
	gimp_progress_update ((double)(i+1) / (double)height);
      
      if ((scan_lines == tile_height) || ((i+1) == height))
	{
	  gimp_pixel_rgn_set_rect (&pixel_rgn, data, 0, i-scan_lines+1,
				   width, scan_lines);
	  scan_lines = 0;
	  dest = data;
	}
    }
  
  g_free (data);
  
  if (err)
1310
    show_message (_("EOF encountered on reading"));
Sven Neumann's avatar
Sven Neumann committed
1311
1312
1313
1314
  
  gimp_drawable_flush (drawable);
  
  return (image_ID);
Elliot Lee's avatar
Elliot Lee committed
1315
1316
1317
1318
}


static gint
Sven Neumann's avatar
Sven Neumann committed
1319
1320
1321
1322
1323
save_index (FILE    *ofp,
            gint32  image_ID,
            gint32  drawable_ID,
            int     grey,
            int     rle)
Elliot Lee's avatar
Elliot Lee committed
1324

Sven Neumann's avatar
Sven Neumann committed
1325
1326
{ 
  int height, width, linepad, i, j;
Elliot Lee's avatar
Elliot Lee committed
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
  int ncols, bw, is_bw, is_wb, bpl;
  int tile_height;
  long tmp = 0;
  unsigned char *cmap, *bwline = NULL;
  unsigned char *data, *src;
  L_SUNFILEHEADER sunhdr;
  unsigned char sun_colormap[256*3];
  static unsigned char sun_bwmap[6] = { 0,255,0,255,0,255 };
  static unsigned char sun_wbmap[6] = { 255,0,255,0,255,0 };
  unsigned char *suncolmap = sun_colormap;
  GPixelRgn pixel_rgn;
  GDrawable *drawable;
  GDrawableType drawable_type;
  WRITE_FUN *write_fun;

  drawable = gimp_drawable_get (drawable_ID);
  drawable_type = gimp_drawable_type (drawable_ID);
  width = drawable->width;
  height = drawable->height;
  tile_height = gimp_tile_height ();
  gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0, width, height, FALSE, FALSE);

  /* allocate a buffer for retrieving information from the pixel region  */
  src = data = (unsigned char *)g_malloc (tile_height * width * drawable->bpp);

  /* Fill SUN-color map */
  if (grey)
    {
Sven Neumann's avatar
Sven Neumann committed
1355
1356
1357
1358
1359
1360
1361
1362
      ncols = 256;
      
      for (j = 0; j < ncols; j++)
	{
	  suncolmap[j] = j;
	  suncolmap[j+ncols] = j;
	  suncolmap[j+ncols*2] = j;
	}
Elliot Lee's avatar
Elliot Lee committed
1363
1364
1365
    }
  else
    {
Sven Neumann's avatar
Sven Neumann committed
1366
1367
1368
1369
1370
1371
1372
1373
      cmap = gimp_image_get_cmap (image_ID, &ncols);
      
      for (j = 0; j < ncols; j++)
	{
	  suncolmap[j] = *(cmap++);
	  suncolmap[j+ncols] = *(cmap++);
	  suncolmap[j+ncols*2] = *(cmap++);
	}
Elliot Lee's avatar
Elliot Lee committed
1374
    }
Sven Neumann's avatar
Sven Neumann committed
1375
  
Elliot Lee's avatar
Elliot Lee committed
1376
1377
  bw = (ncols == 2);   /* Maybe this is a two-colour image */
  if (bw)
Sven Neumann's avatar
Sven Neumann committed
1378
1379
1380
1381
    {
      bwline = g_malloc ((width+7)/8);
      if (bwline == NULL) bw = 0;
    }
Elliot Lee's avatar
Elliot Lee committed
1382
1383
1384

  is_bw = is_wb = 0;
  if (bw)    /* The Sun-OS imagetool generates index 0 for white and */
Sven Neumann's avatar
Sven Neumann committed
1385
1386
1387
1388
1389
    {          /* index 1 for black. Do the same without colourtable. */
      is_bw = (memcmp (suncolmap, sun_bwmap, 6) == 0);
      is_wb = (memcmp (suncolmap, sun_wbmap, 6) == 0);
    }
  
Elliot Lee's avatar
Elliot Lee committed
1390
1391
1392
  /* Number of data bytes per line */
  bpl = bw ?  (width+7)/8 : width;
  linepad = bpl % 2;
Sven Neumann's avatar
Sven Neumann committed
1393
  
Elliot Lee's avatar
Elliot Lee committed
1394
1395
1396
1397
1398
1399
1400
1401
  /* Fill in the SUN header */
  sunhdr.l_ras_magic = RAS_MAGIC;
  sunhdr.l_ras_width = width;
  sunhdr.l_ras_height = height;
  sunhdr.l_ras_depth = bw ? 1 : 8;
  sunhdr.l_ras_length = (bpl+linepad) * height;
  sunhdr.l_ras_type = (rle) ? RAS_TYPE_RLE : RAS_TYPE_STD;
  if (is_bw || is_wb)   /* No colourtable for real b/w images */
Sven Neumann's avatar
Sven Neumann committed
1402
1403
1404
1405
    {
      sunhdr.l_ras_maptype = 0;   /* No colourmap */
      sunhdr.l_ras_maplength = 0; /* Length of colourmap */
    }
Elliot Lee's avatar
Elliot Lee committed
1406
  else
Sven Neumann's avatar
Sven Neumann committed
1407
1408
1409
1410
1411
    {
      sunhdr.l_ras_maptype = 1;   /* RGB colourmap */
      sunhdr.l_ras_maplength = ncols*3; /* Length of colourmap */
    }
  
Elliot Lee's avatar
Elliot Lee committed
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
  write_sun_header (ofp, &sunhdr);

  if (sunhdr.l_ras_maplength > 0)
    write_sun_cols (ofp, &sunhdr, suncolmap);

#define GET_INDEX_TILE(begin) \
  {int scan_lines; \
    scan_lines = (i+tile_height-1 < height) ? tile_height : (height-i); \
    gimp_pixel_rgn_get_rect (&pixel_rgn, begin, 0, i, width, scan_lines); \
    src = begin; }

  if (rle) { write_fun = (WRITE_FUN *)&rle_fwrite; rle_startwrite (ofp); }
Manish Singh's avatar
Manish Singh committed
1424
  else write_fun = (WRITE_FUN *)&my_fwrite;
Elliot Lee's avatar
Elliot Lee committed
1425
1426
1427

  if (bw)  /* Two colour image */
    {
Sven Neumann's avatar
Sven Neumann committed
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
      for (i = 0; i < height; i++)
	{
	  if ((i % tile_height) == 0) GET_INDEX_TILE (data); /* Get more data */
	  byte2bit (src, width, bwline, is_bw);
	  (*write_fun) (bwline, bpl, 1, ofp);
	  if (linepad) (*write_fun) ((char *)&tmp, linepad, 1, ofp);
	  src += width;
	  
	  if ((l_run_mode != RUN_NONINTERACTIVE) && ((i % 20) == 0))
	    gimp_progress_update ((double) i / (double) height);
	}
Elliot Lee's avatar
Elliot Lee committed
1439
1440
1441
    }
  else   /* Colour or grey-image */
    {
Sven Neumann's avatar
Sven Neumann committed
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
      for (i = 0; i < height; i++)
	{
	  if ((i % tile_height) == 0) GET_INDEX_TILE (data); /* Get more data */
	  (*write_fun) ((char *)src, width, 1, ofp);
	  if (linepad) (*write_fun) ((char *)&tmp, linepad, 1, ofp);
	  src += width;
	  
	  if ((l_run_mode != RUN_NONINTERACTIVE) && ((i % 20) == 0))
	    gimp_progress_update ((double) i / (double) height);
	}
Elliot Lee's avatar
Elliot Lee committed
1452
1453
    }

Sven Neumann's avatar
Sven Neumann committed
1454
1455
1456
  if (rle) 
    rle_endwrite (ofp);
  
Elliot Lee's avatar
Elliot Lee committed
1457
1458
  g_free (data);

Sven Neumann's avatar
Sven Neumann committed
1459
1460
1461
  if (bwline) 
    g_free (bwline);
  
Elliot Lee's avatar
Elliot Lee committed
1462
1463
1464
  gimp_drawable_detach (drawable);

  if (ferror (ofp))
Sven Neumann's avatar
Sven Neumann committed
1465
    {
1466
      show_message (_("write error occured"));
Sven Neumann's avatar
Sven Neumann committed
1467
1468
      return (FALSE);
    }
Elliot Lee's avatar
Elliot Lee committed
1469
1470
1471
1472
1473
1474
  return (TRUE);
#undef GET_INDEX_TILE
}


static gint
Sven Neumann's avatar
Sven Neumann committed
1475
1476
1477
1478
save_rgb (FILE   *ofp,
          gint32  image_ID,
          gint32  drawable_ID,
          int     rle)
Elliot Lee's avatar
Elliot Lee committed
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526

{
  int height, width, tile_height, linepad;
  int i, j, bpp;
  unsigned char *data, *src;
  L_SUNFILEHEADER sunhdr;
  GPixelRgn pixel_rgn;
  GDrawable *drawable;
  GDrawableType drawable_type;

  drawable = gimp_drawable_get (drawable_ID);
  drawable_type = gimp_drawable_type (drawable_ID);
  width = drawable->width;
  height = drawable->height;
  tile_height = gimp_tile_height ();
  gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0, width, height, FALSE, FALSE);

  /* allocate a buffer for retrieving information from the pixel region  */
  src = data = (unsigned char *)g_malloc (tile_height * width * drawable->bpp);

/* #define SUNRAS_32 */
#ifdef SUNRAS_32
  bpp = 4;
#else
  bpp = 3;
#endif
  linepad = (width * bpp) % 2;

  /* Fill in the SUN header */
  sunhdr.l_ras_magic = RAS_MAGIC;
  sunhdr.l_ras_width = width;
  sunhdr.l_ras_height = height;
  sunhdr.l_ras_depth = 8 * bpp;
  sunhdr.l_ras_length = (width*bpp + linepad)*height;
  sunhdr.l_ras_type = (rle) ? RAS_TYPE_RLE : RAS_TYPE_STD;
  sunhdr.l_ras_maptype = 0;   /* No colourmap */
  sunhdr.l_ras_maplength = 0; /* Length of colourmap */

  write_sun_header (ofp, &sunhdr);

#define GET_RGB_TILE(begin) \
  {int scan_lines; \
    scan_lines = (i+tile_height-1 < height) ? tile_height : (height-i); \
    gimp_pixel_rgn_get_rect (&pixel_rgn, begin, 0, i, width, scan_lines); \
    src = begin; }

  if (!rle)
    {
Sven Neumann's avatar
Sven Neumann committed
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
      for (i = 0; i < height; i++)
	{
	  if ((i % tile_height) == 0) GET_RGB_TILE (data); /* Get more data */
	  for (j = 0; j < width; j++)
	    {
	      if (bpp == 4) putc (0, ofp);   /* Dummy */
	      putc (*(src+2), ofp);          /* Blue */
	      putc (*(src+1), ofp);          /* Green */
	      putc (*src, ofp);              /* Red */
	      src += 3;
	    }
	  for (j = 0; j < linepad; j++)
	    putc (0, ofp);
	  
	  if ((l_run_mode != RUN_NONINTERACTIVE) && ((i % 20) == 0))
	    gimp_progress_update ((double) i / (double) height);
	}
Elliot Lee's avatar
Elliot Lee committed
1544
1545
1546
    }
  else  /* Write runlength encoded */
    {
Sven Neumann's avatar
Sven Neumann committed
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
      rle_startwrite (ofp);
      
      for (i = 0; i < height; i++)
	{
	  if ((i % tile_height) == 0) GET_RGB_TILE (data); /* Get more data */
	  for (j = 0; j < width; j++)
	    {
	      if (bpp == 4) rle_putc (0, ofp);   /* Dummy */
	      rle_putc (*(src+2), ofp);          /* Blue */
	      rle_putc (*(src+1), ofp);          /* Green */
	      rle_putc (*src, ofp);              /* Red */
	      src += 3;
	    }
	  for (j = 0; j < linepad; j++)
	    rle_putc (0, ofp);
	  
	  if ((l_run_mode != RUN_NONINTERACTIVE) && ((i % 20) == 0))
	    gimp_progress_update ((double) i / (double) height);
	}
      
      rle_endwrite (ofp);
Elliot Lee's avatar
Elliot Lee committed
1568
1569
    }
  g_free (data);
Sven Neumann's avatar
Sven Neumann committed
1570
  
Elliot Lee's avatar
Elliot Lee committed
1571
  gimp_drawable_detach (drawable);
Sven Neumann's avatar
Sven Neumann committed
1572
  
Elliot Lee's avatar
Elliot Lee committed
1573
  if (ferror (ofp))
Sven Neumann's avatar
Sven Neumann committed
1574
    {
1575
      show_message (_("write error occured"));
Sven Neumann's avatar
Sven Neumann committed
1576
1577
      return (FALSE);
    }
Elliot Lee's avatar
Elliot Lee committed
1578
1579
1580
1581
1582
1583
1584
  return (TRUE);
#undef GET_RGB_TILE
}


/*  Save interface functions  */

Sven Neumann's avatar
Sven Neumann committed
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
static void 
init_gtk ()
{
  gchar **argv;
  gint argc;

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


Elliot Lee's avatar
Elliot Lee committed
1600
1601
1602
1603
1604
static gint
save_dialog (void)

{
  GtkWidget *dlg;
Sven Neumann's avatar
Sven Neumann committed
1605
  GtkWidget *hbbox;
Elliot Lee's avatar
Elliot Lee committed
1606
1607
1608
1609
1610
1611
1612
1613
1614
  GtkWidget *button;
  GtkWidget *toggle;
  GtkWidget *frame;
  GtkWidget *toggle_vbox;
  GSList *group;
  gint use_rle = (psvals.rle == TRUE);
  gint use_std = (psvals.rle == FALSE);

  dlg = gtk_dialog_new ();
1615
  gtk_window_set_title (GTK_WINDOW (dlg), _("Save as SUNRAS"));
Elliot Lee's avatar
Elliot Lee committed
1616
1617
1618
1619
1620
1621
  gtk_window_position (GTK_WINDOW (dlg), GTK_WIN_POS_MOUSE);
  gtk_signal_connect (GTK_OBJECT (dlg), "destroy",
                      (GtkSignalFunc) save_close_callback,
                      NULL);

  /*  Action area  */
Sven Neumann's avatar
Sven Neumann committed
1622
1623
1624
1625
1626
1627
1628
1629
  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);
 
  button = gtk_button_new_with_label ("OK");
Elliot Lee's avatar
Elliot Lee committed
1630
1631
  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  gtk_signal_connect (GTK_OBJECT (button), "clicked",
Sven Neumann's avatar
Sven Neumann committed
1632
1633
1634
		      (GtkSignalFunc) save_ok_callback,
		      dlg);
  gtk_box_pack_start (GTK_BOX (hbbox), button, FALSE, FALSE, 0);
Elliot Lee's avatar
Elliot Lee committed
1635
1636
1637
  gtk_widget_grab_default (button);
  gtk_widget_show (button);

Sven Neumann's avatar
Sven Neumann committed
1638
  button = gtk_button_new_with_label ("Cancel");
Elliot Lee's avatar
Elliot Lee committed
1639
1640
  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
Sven Neumann's avatar
Sven Neumann committed
1641
1642
1643
			     (GtkSignalFunc) gtk_widget_destroy,
			     GTK_OBJECT (dlg));
  gtk_box_pack_start (GTK_BOX (hbbox), button, FALSE, FALSE, 0);
Elliot Lee's avatar
Elliot Lee committed
1644
1645
1646
  gtk_widget_show (button);

  /*  file save type  */
1647
  frame = gtk_frame_new (_("Data Formatting"));
Elliot Lee's avatar
Elliot Lee committed
1648
1649
1650
1651
1652
1653
1654
1655
  gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_ETCHED_IN);
  gtk_container_border_width (GTK_CONTAINER (frame), 10);
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), frame, FALSE, TRUE, 0);
  toggle_vbox = gtk_vbox_new (FALSE, 5);
  gtk_container_border_width (GTK_CONTAINER (toggle_vbox), 5);
  gtk_container_add (GTK_CONTAINER (frame), toggle_vbox);

  group = NULL;
1656
  toggle = gtk_radio_button_new_with_label (group, _("RunLengthEncoded"));
Elliot Lee's avatar
Elliot Lee committed
1657
1658
1659
1660
1661
  group = gtk_radio_button_group (GTK_RADIO_BUTTON (toggle));
  gtk_box_pack_start (GTK_BOX (toggle_vbox), toggle, FALSE, FALSE, 0);
  gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
                      (GtkSignalFunc) save_toggle_update,
                      &use_rle);
1662
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), use_rle);
Elliot Lee's avatar
Elliot Lee committed
1663
1664
  gtk_widget_show (toggle);

1665
  toggle = gtk_radio_button_new_with_label (group, _("Standard"));
Elliot Lee's avatar
Elliot Lee committed
1666
1667
1668
1669
1670
  group = gtk_radio_button_group (GTK_RADIO_BUTTON (toggle));
  gtk_box_pack_start (GTK_BOX (toggle_vbox), toggle, FALSE, FALSE, 0);
  gtk_signal_connect (GTK_OBJECT (toggle), "toggled",
                      (GtkSignalFunc) save_toggle_update,
                      &use_std);
1671
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle), use_std);
Elliot Lee's avatar
Elliot Lee committed
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
  gtk_widget_show (toggle);

  gtk_widget_show (toggle_vbox);
  gtk_widget_show (frame);

  gtk_widget_show (dlg);

  gtk_main ();
  gdk_flush ();

  if (use_rle)
    psvals.rle = TRUE;
  else
    psvals.rle = FALSE;

  return psint.run;
}


static void
save_close_callback (GtkWidget *widget,
                     gpointer   data)

{
  gtk_main_quit ();
}


static void
save_ok_callback (GtkWidget *widget,
                  gpointer   data)

{
  psint.run = TRUE;
  gtk_widget_destroy (GTK_WIDGET (data));
}


static void
save_toggle_update (GtkWidget *widget,
                    gpointer   data)

{
  int *toggle_val;

  toggle_val = (int *) data;

  if (GTK_TOGGLE_BUTTON (widget)->active)
    *toggle_val = TRUE;
  else
    *toggle_val = FALSE;
}

Sven Neumann's avatar
Sven Neumann committed
1725
1726
1727
1728
1729
1730
1731
/* Show a message. Where to show it, depends on the runmode */
static void 
show_message (char *message)
{
  if (l_run_mode == RUN_INTERACTIVE)
    gimp_message (message);
  else
1732
    fprintf (stderr, _("sunras: %s\n"), message);
Sven Neumann's avatar
Sven Neumann committed
1733
1734
}

Sven Neumann's avatar
Sven Neumann committed
1735
1736
1737
1738
1739
static int 
my_fwrite (void *ptr, 
	   int   size, 
	   int   nmemb, 
	   FILE *stream)
Manish Singh's avatar
Manish Singh committed
1740
{
Sven Neumann's avatar
Sven Neumann committed
1741
  return fwrite(ptr, size, nmemb, stream);
Manish Singh's avatar
Manish Singh committed
1742
}
Sven Neumann's avatar
Sven Neumann committed
1743
1744
1745
1746