~ubuntu-branches/ubuntu/precise/gimp/precise-security

« back to all changes in this revision

Viewing changes to .pc/CVE-2013-1913.patch/plug-ins/common/file-xwd.c

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2013-12-06 13:32:56 UTC
  • Revision ID: package-import@ubuntu.com-20131206133256-8xqk4mo8qxr2zq6m
Tags: 2.6.12-1ubuntu1.3
* SECURITY UPDATE: denial of service and possible code execution via
  huge color maps in xwd plugin
  - debian/patches/CVE-2013-1913.patch: limit number of color map entries
    in plug-ins/common/file-xwd.c.
  - CVE-2013-1913
* SECURITY UPDATE: denial of service and possible code execution via
  large number of color map entries in xwd plugin
  - debian/patches/CVE-2013-1978.patch: validate number of color map
    entries in plug-ins/common/file-xwd.c
  - CVE-2013-1978

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GIMP - The GNU Image Manipulation Program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 * XWD reading and writing code Copyright (C) 1996 Peter Kirchgessner
 
4
 * (email: peter@kirchgessner.net, WWW: http://www.kirchgessner.net)
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
19
 *
 
20
 */
 
21
 
 
22
/*
 
23
 * XWD-input/output was written by Peter Kirchgessner (peter@kirchgessner.net)
 
24
 * Examples from mainly used UNIX-systems have been used for testing.
 
25
 * If a file does not work, please return a small (!!) compressed example.
 
26
 * Currently the following formats are supported:
 
27
 *    pixmap_format | pixmap_depth | bits_per_pixel
 
28
 *    ---------------------------------------------
 
29
 *          0       |        1     |       1
 
30
 *          1       |    1,...,24  |       1
 
31
 *          2       |        1     |       1
 
32
 *          2       |    1,...,8   |       8
 
33
 *          2       |    1,...,16  |      16
 
34
 *          2       |    1,...,24  |      24
 
35
 *          2       |    1,...,24  |      32
 
36
 */
 
37
/* Event history:
 
38
 * PK = Peter Kirchgessner, ME = Mattias Engdeg�rd
 
39
 * V 1.00, PK, xx-Aug-96: First try
 
40
 * V 1.01, PK, 03-Sep-96: Check for bitmap_bit_order
 
41
 * V 1.90, PK, 17-Mar-97: Upgrade to work with GIMP V0.99
 
42
 *                        Use visual class 3 to write indexed image
 
43
 *                        Set gimp b/w-colormap if no xwdcolormap present
 
44
 * V 1.91, PK, 05-Apr-97: Return all arguments, even in case of an error
 
45
 * V 1.92, PK, 12-Oct-97: No progress bars for non-interactive mode
 
46
 * V 1.93, PK, 11-Apr-98: Fix problem with overwriting memory
 
47
 * V 1.94, ME, 27-Feb-00: Remove superfluous little-endian support (format is
 
48
                          specified as big-endian). Trim magic header
 
49
 * V 1.95, PK, 02-Jul-01: Fix problem with 8 bit image
 
50
 */
 
51
 
 
52
#include "config.h"
 
53
 
 
54
#include <errno.h>
 
55
#include <string.h>
 
56
 
 
57
#include <glib/gstdio.h>
 
58
 
 
59
#include <libgimp/gimp.h>
 
60
#include <libgimp/gimpui.h>
 
61
 
 
62
#include "libgimp/stdplugins-intl.h"
 
63
 
 
64
 
 
65
#define LOAD_PROC      "file-xwd-load"
 
66
#define SAVE_PROC      "file-xwd-save"
 
67
#define PLUG_IN_BINARY "file-xwd"
 
68
 
 
69
 
 
70
typedef gulong  L_CARD32;
 
71
typedef gushort L_CARD16;
 
72
typedef guchar  L_CARD8;
 
73
 
 
74
typedef struct
 
75
{
 
76
  L_CARD32 l_header_size;    /* Header size */
 
77
 
 
78
  L_CARD32 l_file_version;   /* File version (7) */
 
79
  L_CARD32 l_pixmap_format;  /* Image type */
 
80
  L_CARD32 l_pixmap_depth;   /* Number of planes */
 
81
  L_CARD32 l_pixmap_width;   /* Image width */
 
82
  L_CARD32 l_pixmap_height;  /* Image height */
 
83
  L_CARD32 l_xoffset;        /* x-offset (0 ?) */
 
84
  L_CARD32 l_byte_order;     /* Byte ordering */
 
85
 
 
86
  L_CARD32 l_bitmap_unit;
 
87
  L_CARD32 l_bitmap_bit_order; /* Bit order */
 
88
  L_CARD32 l_bitmap_pad;
 
89
  L_CARD32 l_bits_per_pixel; /* Number of bits per pixel */
 
90
 
 
91
  L_CARD32 l_bytes_per_line; /* Number of bytes per scanline */
 
92
  L_CARD32 l_visual_class;   /* Visual class */
 
93
  L_CARD32 l_red_mask;       /* Red mask */
 
94
  L_CARD32 l_green_mask;     /* Green mask */
 
95
  L_CARD32 l_blue_mask;      /* Blue mask */
 
96
  L_CARD32 l_bits_per_rgb;   /* Number of bits per RGB-part */
 
97
  L_CARD32 l_colormap_entries; /* Number of colors in color table (?) */
 
98
  L_CARD32 l_ncolors;        /* Number of xwdcolor structures */
 
99
  L_CARD32 l_window_width;   /* Window width */
 
100
  L_CARD32 l_window_height;  /* Window height */
 
101
  L_CARD32 l_window_x;       /* Window position x */
 
102
  L_CARD32 l_window_y;       /* Window position y */
 
103
  L_CARD32 l_window_bdrwidth;/* Window border width */
 
104
} L_XWDFILEHEADER;
 
105
 
 
106
typedef struct
 
107
{
 
108
  L_CARD32 l_pixel;          /* Color index */
 
109
  L_CARD16 l_red, l_green, l_blue;  /* RGB-values */
 
110
  L_CARD8  l_flags, l_pad;
 
111
} L_XWDCOLOR;
 
112
 
 
113
 
 
114
/* Some structures for mapping up to 32bit-pixel */
 
115
/* values which are kept in the XWD-Colormap */
 
116
 
 
117
#define MAPPERBITS 12
 
118
#define MAPPERMASK ((1 << MAPPERBITS)-1)
 
119
 
 
120
typedef struct
 
121
{
 
122
  L_CARD32 pixel_val;
 
123
  guchar   red;
 
124
  guchar   green;
 
125
  guchar   blue;
 
126
} PMAP;
 
127
 
 
128
typedef struct
 
129
{
 
130
  gint   npixel; /* Number of pixel values in map */
 
131
  guchar pixel_in_map[1 << MAPPERBITS];
 
132
  PMAP   pmap[256];
 
133
} PIXEL_MAP;
 
134
 
 
135
#define XWDHDR_PAD   0  /* Total number of padding bytes for XWD header */
 
136
#define XWDCOL_PAD   0  /* Total number of padding bytes for each XWD color */
 
137
 
 
138
/* Declare some local functions.
 
139
 */
 
140
static void   query               (void);
 
141
static void   run                 (const gchar      *name,
 
142
                                   gint              nparams,
 
143
                                   const GimpParam  *param,
 
144
                                   gint             *nreturn_vals,
 
145
                                   GimpParam       **return_vals);
 
146
 
 
147
static gint32 load_image          (const gchar      *filename,
 
148
                                   GError          **error);
 
149
static gint   save_image          (const gchar      *filename,
 
150
                                   gint32            image_ID,
 
151
                                   gint32            drawable_ID,
 
152
                                   GError          **error);
 
153
static gint32 create_new_image    (const gchar      *filename,
 
154
                                   guint             width,
 
155
                                   guint             height,
 
156
                                   GimpImageBaseType type,
 
157
                                   gint32           *layer_ID,
 
158
                                   GimpDrawable    **drawable,
 
159
                                   GimpPixelRgn     *pixel_rgn);
 
160
 
 
161
static int      set_pixelmap      (gint,
 
162
                                   L_XWDCOLOR *,
 
163
                                   PIXEL_MAP *);
 
164
static gboolean get_pixelmap      (L_CARD32,
 
165
                                   PIXEL_MAP *,
 
166
                                   guchar *,
 
167
                                   guchar *,
 
168
                                   guchar *);
 
169
static void   set_bw_color_table  (gint32);
 
170
static void   set_color_table     (gint32,
 
171
                                   L_XWDFILEHEADER *,
 
172
                                   L_XWDCOLOR *);
 
173
 
 
174
static gint32 load_xwd_f2_d1_b1   (const gchar *,
 
175
                                   FILE *,
 
176
                                   L_XWDFILEHEADER *,
 
177
                                   L_XWDCOLOR *);
 
178
static gint32 load_xwd_f2_d8_b8   (const gchar *,
 
179
                                   FILE *,
 
180
                                   L_XWDFILEHEADER *,
 
181
                                   L_XWDCOLOR *);
 
182
static gint32 load_xwd_f2_d16_b16 (const gchar *,
 
183
                                   FILE *,
 
184
                                   L_XWDFILEHEADER *,
 
185
                                   L_XWDCOLOR *);
 
186
static gint32 load_xwd_f2_d24_b32 (const gchar *,
 
187
                                   FILE *,
 
188
                                   L_XWDFILEHEADER *,
 
189
                                   L_XWDCOLOR *,
 
190
                                   GError **);
 
191
static gint32 load_xwd_f1_d24_b1  (const gchar *,
 
192
                                   FILE *,
 
193
                                   L_XWDFILEHEADER *,
 
194
                                   L_XWDCOLOR *,
 
195
                                   GError **);
 
196
 
 
197
static L_CARD32 read_card32  (FILE *,
 
198
                              gint *);
 
199
static L_CARD16 read_card16  (FILE *,
 
200
                              gint *);
 
201
static L_CARD8  read_card8   (FILE *,
 
202
                              gint *);
 
203
 
 
204
static void write_card32     (FILE *,
 
205
                              L_CARD32);
 
206
static void write_card16     (FILE *,
 
207
                              L_CARD32);
 
208
static void write_card8      (FILE *,
 
209
                              L_CARD32);
 
210
 
 
211
static void read_xwd_header  (FILE *,
 
212
                              L_XWDFILEHEADER *);
 
213
static void write_xwd_header (FILE *,
 
214
                              L_XWDFILEHEADER *);
 
215
static void read_xwd_cols    (FILE *,
 
216
                              L_XWDFILEHEADER *,
 
217
                              L_XWDCOLOR *);
 
218
static void write_xwd_cols   (FILE *,
 
219
                              L_XWDFILEHEADER *,
 
220
                              L_XWDCOLOR *);
 
221
 
 
222
static gint save_index       (FILE *,
 
223
                              gint32,
 
224
                              gint32,
 
225
                              gint);
 
226
static gint save_rgb         (FILE *,
 
227
                              gint32,
 
228
                              gint32);
 
229
 
 
230
 
 
231
const GimpPlugInInfo PLUG_IN_INFO =
 
232
{
 
233
  NULL,  /* init_proc  */
 
234
  NULL,  /* quit_proc  */
 
235
  query, /* query_proc */
 
236
  run,   /* run_proc   */
 
237
};
 
238
 
 
239
 
 
240
/* The run mode */
 
241
static GimpRunMode l_run_mode;
 
242
 
 
243
 
 
244
MAIN ()
 
245
 
 
246
 
 
247
static void
 
248
query (void)
 
249
 
 
250
{
 
251
  static const GimpParamDef load_args[] =
 
252
  {
 
253
    { GIMP_PDB_INT32,  "run-mode",     "Interactive, non-interactive" },
 
254
    { GIMP_PDB_STRING, "filename",     "The name of the file to load" },
 
255
    { GIMP_PDB_STRING, "raw-filename", "The name of the file to load" }
 
256
  };
 
257
 
 
258
  static const GimpParamDef load_return_vals[] =
 
259
  {
 
260
    { GIMP_PDB_IMAGE,  "image",        "Output image" }
 
261
  };
 
262
 
 
263
  static const GimpParamDef save_args[] =
 
264
  {
 
265
    { GIMP_PDB_INT32,    "run-mode",     "Interactive, non-interactive" },
 
266
    { GIMP_PDB_IMAGE,    "image",        "Input image" },
 
267
    { GIMP_PDB_DRAWABLE, "drawable",     "Drawable to save" },
 
268
    { GIMP_PDB_STRING,   "filename",     "The name of the file to save the image in" },
 
269
    { GIMP_PDB_STRING,   "raw-filename", "The name of the file to save the image in" }
 
270
  };
 
271
 
 
272
  gimp_install_procedure (LOAD_PROC,
 
273
                          "Loads files in the XWD (X Window Dump) format",
 
274
                          "Loads files in the XWD (X Window Dump) format. "
 
275
                          "XWD image files are produced by the program xwd. "
 
276
                          "Xwd is an X Window System window dumping utility.",
 
277
                          "Peter Kirchgessner",
 
278
                          "Peter Kirchgessner",
 
279
                          "1996",
 
280
                          N_("X window dump"),
 
281
                          NULL,
 
282
                          GIMP_PLUGIN,
 
283
                          G_N_ELEMENTS (load_args),
 
284
                          G_N_ELEMENTS (load_return_vals),
 
285
                          load_args, load_return_vals);
 
286
 
 
287
  gimp_register_file_handler_mime (LOAD_PROC, "image/x-xwindowdump");
 
288
  gimp_register_magic_load_handler (LOAD_PROC,
 
289
                                    "xwd",
 
290
                                    "",
 
291
                                    "4,long,0x00000007");
 
292
 
 
293
  gimp_install_procedure (SAVE_PROC,
 
294
                          "Saves files in the XWD (X Window Dump) format",
 
295
                          "XWD saving handles all image types except "
 
296
                          "those with alpha channels.",
 
297
                          "Peter Kirchgessner",
 
298
                          "Peter Kirchgessner",
 
299
                          "1996",
 
300
                          N_("X window dump"),
 
301
                          "RGB, GRAY, INDEXED",
 
302
                          GIMP_PLUGIN,
 
303
                          G_N_ELEMENTS (save_args), 0,
 
304
                          save_args, NULL);
 
305
 
 
306
  gimp_register_file_handler_mime (SAVE_PROC, "image/x-xwindowdump");
 
307
  gimp_register_save_handler (SAVE_PROC, "xwd", "");
 
308
}
 
309
 
 
310
 
 
311
static void
 
312
run (const gchar      *name,
 
313
     gint              nparams,
 
314
     const GimpParam  *param,
 
315
     gint             *nreturn_vals,
 
316
     GimpParam       **return_vals)
 
317
{
 
318
  static GimpParam   values[2];
 
319
  GimpRunMode        run_mode;
 
320
  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
 
321
  gint32             image_ID;
 
322
  gint32             drawable_ID;
 
323
  GimpExportReturn   export = GIMP_EXPORT_CANCEL;
 
324
  GError            *error  = NULL;
 
325
 
 
326
  l_run_mode = run_mode = param[0].data.d_int32;
 
327
 
 
328
  INIT_I18N ();
 
329
 
 
330
  *nreturn_vals = 1;
 
331
  *return_vals  = values;
 
332
 
 
333
  values[0].type          = GIMP_PDB_STATUS;
 
334
  values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
 
335
 
 
336
  if (strcmp (name, LOAD_PROC) == 0)
 
337
    {
 
338
      image_ID = load_image (param[1].data.d_string, &error);
 
339
 
 
340
      if (image_ID != -1)
 
341
        {
 
342
          *nreturn_vals = 2;
 
343
          values[1].type         = GIMP_PDB_IMAGE;
 
344
          values[1].data.d_image = image_ID;
 
345
        }
 
346
      else
 
347
        {
 
348
          status = GIMP_PDB_EXECUTION_ERROR;
 
349
        }
 
350
    }
 
351
  else if (strcmp (name, SAVE_PROC) == 0)
 
352
    {
 
353
      image_ID    = param[1].data.d_int32;
 
354
      drawable_ID = param[2].data.d_int32;
 
355
 
 
356
      /*  eventually export the image */
 
357
      switch (run_mode)
 
358
        {
 
359
        case GIMP_RUN_INTERACTIVE:
 
360
        case GIMP_RUN_WITH_LAST_VALS:
 
361
          gimp_ui_init (PLUG_IN_BINARY, FALSE);
 
362
          export = gimp_export_image (&image_ID, &drawable_ID, "XWD",
 
363
                                      (GIMP_EXPORT_CAN_HANDLE_RGB |
 
364
                                       GIMP_EXPORT_CAN_HANDLE_GRAY |
 
365
                                       GIMP_EXPORT_CAN_HANDLE_INDEXED));
 
366
          if (export == GIMP_EXPORT_CANCEL)
 
367
            {
 
368
              values[0].data.d_status = GIMP_PDB_CANCEL;
 
369
              return;
 
370
            }
 
371
          break;
 
372
 
 
373
        default:
 
374
          break;
 
375
        }
 
376
 
 
377
      switch (run_mode)
 
378
        {
 
379
        case GIMP_RUN_INTERACTIVE:
 
380
        case GIMP_RUN_WITH_LAST_VALS:
 
381
          /* No additional data to retrieve */
 
382
          break;
 
383
 
 
384
        case GIMP_RUN_NONINTERACTIVE:
 
385
          /*  Make sure all the arguments are there!  */
 
386
          if (nparams != 5)
 
387
            status = GIMP_PDB_CALLING_ERROR;
 
388
          break;
 
389
 
 
390
        default:
 
391
          break;
 
392
        }
 
393
 
 
394
      if (status == GIMP_PDB_SUCCESS)
 
395
        {
 
396
          if (! save_image (param[3].data.d_string, image_ID, drawable_ID,
 
397
                            &error))
 
398
            {
 
399
              status = GIMP_PDB_EXECUTION_ERROR;
 
400
            }
 
401
        }
 
402
 
 
403
      if (export == GIMP_EXPORT_EXPORT)
 
404
        gimp_image_delete (image_ID);
 
405
    }
 
406
  else
 
407
    {
 
408
      status = GIMP_PDB_CANCEL;
 
409
    }
 
410
 
 
411
  if (status != GIMP_PDB_SUCCESS && error)
 
412
    {
 
413
      *nreturn_vals = 2;
 
414
      values[1].type          = GIMP_PDB_STRING;
 
415
      values[1].data.d_string = error->message;
 
416
    }
 
417
 
 
418
  values[0].data.d_status = status;
 
419
}
 
420
 
 
421
static gint32
 
422
load_image (const gchar  *filename,
 
423
            GError      **error)
 
424
{
 
425
  FILE            *ifp;
 
426
  gint             depth, bpp;
 
427
  gint32           image_ID;
 
428
  L_XWDFILEHEADER  xwdhdr;
 
429
  L_XWDCOLOR      *xwdcolmap = NULL;
 
430
 
 
431
  ifp = g_fopen (filename, "rb");
 
432
  if (!ifp)
 
433
    {
 
434
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
 
435
                   _("Could not open '%s' for reading: %s"),
 
436
                   gimp_filename_to_utf8 (filename), g_strerror (errno));
 
437
      return -1;
 
438
    }
 
439
 
 
440
  read_xwd_header (ifp, &xwdhdr);
 
441
  if (xwdhdr.l_file_version != 7)
 
442
    {
 
443
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
 
444
                   _("Could not read XWD header from '%s'"),
 
445
                   gimp_filename_to_utf8 (filename));
 
446
      fclose (ifp);
 
447
      return -1;
 
448
    }
 
449
 
 
450
#ifdef XWD_COL_WAIT_DEBUG
 
451
  {
 
452
    int k = 1;
 
453
 
 
454
    while (k)
 
455
      k = k;
 
456
  }
 
457
#endif
 
458
 
 
459
  /* Position to start of XWDColor structures */
 
460
  fseek (ifp, (long)xwdhdr.l_header_size, SEEK_SET);
 
461
 
 
462
  if (xwdhdr.l_colormap_entries > 0)
 
463
    {
 
464
      xwdcolmap = g_new (L_XWDCOLOR, xwdhdr.l_colormap_entries);
 
465
 
 
466
      read_xwd_cols (ifp, &xwdhdr, xwdcolmap);
 
467
 
 
468
#ifdef XWD_COL_DEBUG
 
469
      {
 
470
        int j;
 
471
        printf ("File %s\n",filename);
 
472
        for (j = 0; j < xwdhdr.l_colormap_entries; j++)
 
473
          printf ("Entry 0x%08lx: 0x%04lx,  0x%04lx, 0x%04lx, %d\n",
 
474
                  (long)xwdcolmap[j].l_pixel,(long)xwdcolmap[j].l_red,
 
475
                  (long)xwdcolmap[j].l_green,(long)xwdcolmap[j].l_blue,
 
476
                  (int)xwdcolmap[j].l_flags);
 
477
      }
 
478
#endif
 
479
 
 
480
      if (xwdhdr.l_file_version != 7)
 
481
        {
 
482
          g_message (_("Can't read color entries"));
 
483
          g_free (xwdcolmap);
 
484
          fclose (ifp);
 
485
          return (-1);
 
486
        }
 
487
    }
 
488
 
 
489
  if (xwdhdr.l_pixmap_width <= 0)
 
490
    {
 
491
      g_message (_("'%s':\nNo image width specified"),
 
492
                 gimp_filename_to_utf8 (filename));
 
493
      fclose (ifp);
 
494
      return (-1);
 
495
    }
 
496
 
 
497
  if (xwdhdr.l_pixmap_width > GIMP_MAX_IMAGE_SIZE
 
498
      || xwdhdr.l_bytes_per_line > GIMP_MAX_IMAGE_SIZE * 3)
 
499
    {
 
500
      g_message (_("'%s':\nImage width is larger than GIMP can handle"),
 
501
                 gimp_filename_to_utf8 (filename));
 
502
      fclose (ifp);
 
503
      return (-1);
 
504
    }
 
505
 
 
506
  if (xwdhdr.l_pixmap_height <= 0)
 
507
    {
 
508
      g_message (_("'%s':\nNo image height specified"),
 
509
                 gimp_filename_to_utf8 (filename));
 
510
      fclose (ifp);
 
511
      return (-1);
 
512
    }
 
513
 
 
514
  if (xwdhdr.l_pixmap_height > GIMP_MAX_IMAGE_SIZE)
 
515
    {
 
516
      g_message (_("'%s':\nImage height is larger than GIMP can handle"),
 
517
                 gimp_filename_to_utf8 (filename));
 
518
      fclose (ifp);
 
519
      return (-1);
 
520
    }
 
521
 
 
522
  gimp_progress_init_printf (_("Opening '%s'"),
 
523
                             gimp_filename_to_utf8 (filename));
 
524
 
 
525
  depth = xwdhdr.l_pixmap_depth;
 
526
  bpp   = xwdhdr.l_bits_per_pixel;
 
527
 
 
528
  image_ID = -1;
 
529
  switch (xwdhdr.l_pixmap_format)
 
530
    {
 
531
    case 0:    /* Single plane bitmap */
 
532
      if ((depth == 1) && (bpp == 1))
 
533
        { /* Can be performed by format 2 loader */
 
534
          image_ID = load_xwd_f2_d1_b1 (filename, ifp, &xwdhdr, xwdcolmap);
 
535
        }
 
536
      break;
 
537
 
 
538
    case 1:    /* Single plane pixmap */
 
539
      if ((depth <= 24) && (bpp == 1))
 
540
        {
 
541
          image_ID = load_xwd_f1_d24_b1 (filename, ifp, &xwdhdr, xwdcolmap,
 
542
                                         error);
 
543
        }
 
544
      break;
 
545
 
 
546
    case 2:    /* Multiplane pixmaps */
 
547
      if ((depth == 1) && (bpp == 1))
 
548
        {
 
549
          image_ID = load_xwd_f2_d1_b1 (filename, ifp, &xwdhdr, xwdcolmap);
 
550
        }
 
551
      else if ((depth <= 8) && (bpp == 8))
 
552
        {
 
553
          image_ID = load_xwd_f2_d8_b8 (filename, ifp, &xwdhdr, xwdcolmap);
 
554
        }
 
555
      else if ((depth <= 16) && (bpp == 16))
 
556
        {
 
557
          image_ID = load_xwd_f2_d16_b16 (filename, ifp, &xwdhdr, xwdcolmap);
 
558
        }
 
559
      else if ((depth <= 24) && ((bpp == 24) || (bpp == 32)))
 
560
        {
 
561
          image_ID = load_xwd_f2_d24_b32 (filename, ifp, &xwdhdr, xwdcolmap,
 
562
                                          error);
 
563
        }
 
564
      break;
 
565
    }
 
566
 
 
567
  fclose (ifp);
 
568
 
 
569
  if (xwdcolmap)
 
570
    g_free (xwdcolmap);
 
571
 
 
572
  if (image_ID == -1 && ! (error && *error))
 
573
    g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
 
574
                 _("XWD-file %s has format %d, depth %d and bits per pixel %d. "
 
575
                   "Currently this is not supported."),
 
576
                 gimp_filename_to_utf8 (filename),
 
577
                 (gint) xwdhdr.l_pixmap_format, depth, bpp);
 
578
 
 
579
  return image_ID;
 
580
}
 
581
 
 
582
static gint
 
583
save_image (const gchar  *filename,
 
584
            gint32        image_ID,
 
585
            gint32        drawable_ID,
 
586
            GError      **error)
 
587
{
 
588
  FILE          *ofp;
 
589
  GimpImageType  drawable_type;
 
590
  gint           retval;
 
591
 
 
592
  drawable_type = gimp_drawable_type (drawable_ID);
 
593
 
 
594
  /*  Make sure we're not saving an image with an alpha channel  */
 
595
  if (gimp_drawable_has_alpha (drawable_ID))
 
596
    {
 
597
      g_message (_("Cannot save images with alpha channels."));
 
598
      return FALSE;
 
599
    }
 
600
 
 
601
  switch (drawable_type)
 
602
    {
 
603
    case GIMP_INDEXED_IMAGE:
 
604
    case GIMP_GRAY_IMAGE:
 
605
    case GIMP_RGB_IMAGE:
 
606
      break;
 
607
    default:
 
608
      g_message (_("Cannot operate on unknown image types."));
 
609
      return FALSE;
 
610
      break;
 
611
    }
 
612
 
 
613
  /* Open the output file. */
 
614
  ofp = g_fopen (filename, "wb");
 
615
  if (! ofp)
 
616
    {
 
617
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
 
618
                   _("Could not open '%s' for writing: %s"),
 
619
                   gimp_filename_to_utf8 (filename), g_strerror (errno));
 
620
      return FALSE;
 
621
    }
 
622
 
 
623
  gimp_progress_init_printf (_("Saving '%s'"),
 
624
                             gimp_filename_to_utf8 (filename));
 
625
 
 
626
  if (drawable_type == GIMP_INDEXED_IMAGE)
 
627
    retval = save_index (ofp, image_ID, drawable_ID, 0);
 
628
  else if (drawable_type == GIMP_GRAY_IMAGE)
 
629
    retval = save_index (ofp, image_ID, drawable_ID, 1);
 
630
  else if (drawable_type == GIMP_RGB_IMAGE)
 
631
    retval = save_rgb (ofp, image_ID, drawable_ID);
 
632
  else
 
633
    retval = FALSE;
 
634
 
 
635
  fclose (ofp);
 
636
 
 
637
  return retval;
 
638
}
 
639
 
 
640
 
 
641
static L_CARD32
 
642
read_card32 (FILE *ifp,
 
643
             int  *err)
 
644
 
 
645
{
 
646
  L_CARD32 c;
 
647
 
 
648
  c =  (((L_CARD32) (getc (ifp))) << 24);
 
649
  c |= (((L_CARD32) (getc (ifp))) << 16);
 
650
  c |= (((L_CARD32) (getc (ifp))) << 8);
 
651
  c |= ((L_CARD32) (*err = getc (ifp)));
 
652
 
 
653
  *err = (*err < 0);
 
654
 
 
655
  return c;
 
656
}
 
657
 
 
658
 
 
659
static L_CARD16
 
660
read_card16 (FILE *ifp,
 
661
             int  *err)
 
662
 
 
663
{
 
664
  L_CARD16 c;
 
665
 
 
666
  c =  (((L_CARD16) (getc (ifp))) << 8);
 
667
  c |= ((L_CARD16) (*err = getc (ifp)));
 
668
 
 
669
  *err = (*err < 0);
 
670
 
 
671
  return c;
 
672
}
 
673
 
 
674
 
 
675
static L_CARD8
 
676
read_card8 (FILE *ifp,
 
677
            int  *err)
 
678
{
 
679
  L_CARD8 c;
 
680
 
 
681
  c = ((L_CARD8) (*err = getc (ifp)));
 
682
 
 
683
  *err = (*err < 0);
 
684
 
 
685
  return c;
 
686
}
 
687
 
 
688
static void
 
689
write_card32 (FILE     *ofp,
 
690
              L_CARD32  c)
 
691
{
 
692
  putc ((int) ((c >> 24) & 0xff), ofp);
 
693
  putc ((int) ((c >> 16) & 0xff), ofp);
 
694
  putc ((int) ((c >> 8) & 0xff), ofp);
 
695
  putc ((int) ((c) & 0xff), ofp);
 
696
}
 
697
 
 
698
 
 
699
static void
 
700
write_card16 (FILE     *ofp,
 
701
              L_CARD32  c)
 
702
{
 
703
  putc ((int) ((c >> 8) & 0xff), ofp);
 
704
  putc ((int) ((c) & 0xff), ofp);
 
705
}
 
706
 
 
707
 
 
708
static void
 
709
write_card8 (FILE     *ofp,
 
710
             L_CARD32  c)
 
711
{
 
712
  putc ((int) ((c) & 0xff), ofp);
 
713
}
 
714
 
 
715
 
 
716
static void
 
717
read_xwd_header (FILE            *ifp,
 
718
                 L_XWDFILEHEADER *xwdhdr)
 
719
{
 
720
  gint      j, err;
 
721
  L_CARD32 *cp;
 
722
 
 
723
  cp = (L_CARD32 *) xwdhdr;
 
724
 
 
725
  /* Read in all 32-bit values of the header and check for byte order */
 
726
  for (j = 0; j < sizeof (L_XWDFILEHEADER) / sizeof(xwdhdr->l_file_version); j++)
 
727
    {
 
728
      *(cp++) = read_card32 (ifp, &err);
 
729
 
 
730
      if (err)
 
731
        break;
 
732
    }
 
733
 
 
734
  if (err)
 
735
    xwdhdr->l_file_version = 0;  /* Not a valid XWD-file */
 
736
}
 
737
 
 
738
 
 
739
/* Write out an XWD-fileheader. The header size is calculated here */
 
740
static void
 
741
write_xwd_header (FILE            *ofp,
 
742
                  L_XWDFILEHEADER *xwdhdr)
 
743
 
 
744
{
 
745
  gint      j, hdrpad, hdr_entries;
 
746
  L_CARD32 *cp;
 
747
 
 
748
  hdrpad = XWDHDR_PAD;
 
749
  hdr_entries = sizeof (L_XWDFILEHEADER) / sizeof(xwdhdr->l_file_version);
 
750
  xwdhdr->l_header_size = hdr_entries * 4 + hdrpad;
 
751
 
 
752
  cp = (L_CARD32 *) xwdhdr;
 
753
 
 
754
  /* Write out all 32-bit values of the header and check for byte order */
 
755
  for (j = 0; j < sizeof (L_XWDFILEHEADER) / sizeof(xwdhdr->l_file_version); j++)
 
756
    {
 
757
      write_card32 (ofp, *(cp++));
 
758
    }
 
759
 
 
760
  /* Add padding bytes after XWD header */
 
761
  for (j = 0; j < hdrpad; j++)
 
762
    write_card8 (ofp, (L_CARD32) 0);
 
763
}
 
764
 
 
765
 
 
766
static void
 
767
read_xwd_cols (FILE            *ifp,
 
768
               L_XWDFILEHEADER *xwdhdr,
 
769
               L_XWDCOLOR      *colormap)
 
770
{
 
771
  gint  j, err = 0;
 
772
  gint  flag_is_bad, index_is_bad;
 
773
  gint  indexed = (xwdhdr->l_pixmap_depth <= 8);
 
774
  glong colmappos = ftell (ifp);
 
775
 
 
776
  /* Read in XWD-Color structures (the usual case) */
 
777
  flag_is_bad = index_is_bad = 0;
 
778
  for (j = 0; j < xwdhdr->l_ncolors; j++)
 
779
    {
 
780
      colormap[j].l_pixel = read_card32 (ifp, &err);
 
781
 
 
782
      colormap[j].l_red   = read_card16 (ifp, &err);
 
783
      colormap[j].l_green = read_card16 (ifp, &err);
 
784
      colormap[j].l_blue  = read_card16 (ifp, &err);
 
785
 
 
786
      colormap[j].l_flags = read_card8 (ifp, &err);
 
787
      colormap[j].l_pad   = read_card8 (ifp, &err);
 
788
 
 
789
      if (indexed && (colormap[j].l_pixel > 255))
 
790
        index_is_bad++;
 
791
 
 
792
      if (err)
 
793
        break;
 
794
    }
 
795
 
 
796
  if (err)        /* Not a valid XWD-file ? */
 
797
    xwdhdr->l_file_version = 0;
 
798
  if (err || ((flag_is_bad == 0) && (index_is_bad == 0)))
 
799
    return;
 
800
 
 
801
  /* Read in XWD-Color structures (with 4 bytes inserted infront of RGB) */
 
802
  fseek (ifp, colmappos, SEEK_SET);
 
803
  flag_is_bad = index_is_bad = 0;
 
804
  for (j = 0; j < xwdhdr->l_ncolors; j++)
 
805
    {
 
806
      colormap[j].l_pixel = read_card32 (ifp, &err);
 
807
 
 
808
      read_card32 (ifp, &err);  /* Empty bytes on Alpha OSF */
 
809
 
 
810
      colormap[j].l_red   = read_card16 (ifp, &err);
 
811
      colormap[j].l_green = read_card16 (ifp, &err);
 
812
      colormap[j].l_blue  = read_card16 (ifp, &err);
 
813
 
 
814
      colormap[j].l_flags = read_card8 (ifp, &err);
 
815
      colormap[j].l_pad   = read_card8 (ifp, &err);
 
816
 
 
817
      if (indexed && (colormap[j].l_pixel > 255))
 
818
        index_is_bad++;
 
819
 
 
820
      if (err)
 
821
        break;
 
822
    }
 
823
 
 
824
  if (err)        /* Not a valid XWD-file ? */
 
825
    xwdhdr->l_file_version = 0;
 
826
  if (err || ((flag_is_bad == 0) && (index_is_bad == 0)))
 
827
    return;
 
828
 
 
829
  /* Read in XWD-Color structures (with 2 bytes inserted infront of RGB) */
 
830
  fseek (ifp, colmappos, SEEK_SET);
 
831
  flag_is_bad = index_is_bad = 0;
 
832
  for (j = 0; j < xwdhdr->l_ncolors; j++)
 
833
    {
 
834
      colormap[j].l_pixel = read_card32 (ifp, &err);
 
835
 
 
836
      read_card16 (ifp, &err);  /* Empty bytes (from where ?) */
 
837
 
 
838
      colormap[j].l_red   = read_card16 (ifp, &err);
 
839
      colormap[j].l_green = read_card16 (ifp, &err);
 
840
      colormap[j].l_blue  = read_card16 (ifp, &err);
 
841
 
 
842
      colormap[j].l_flags = read_card8 (ifp, &err);
 
843
      colormap[j].l_pad   = read_card8 (ifp, &err);
 
844
 
 
845
      /* if ((colormap[j].l_flags == 0) || (colormap[j].l_flags > 7))
 
846
         flag_is_bad++; */
 
847
 
 
848
      if (indexed && (colormap[j].l_pixel > 255))
 
849
        index_is_bad++;
 
850
 
 
851
      if (err)
 
852
        break;
 
853
    }
 
854
 
 
855
  if (err)        /* Not a valid XWD-file ? */
 
856
    xwdhdr->l_file_version = 0;
 
857
  if (err || ((flag_is_bad == 0) && (index_is_bad == 0)))
 
858
    return;
 
859
 
 
860
  /* Read in XWD-Color structures (every value is 8 bytes from a CRAY) */
 
861
  fseek (ifp, colmappos, SEEK_SET);
 
862
  flag_is_bad = index_is_bad = 0;
 
863
  for (j = 0; j < xwdhdr->l_ncolors; j++)
 
864
    {
 
865
      read_card32 (ifp, &err);
 
866
      colormap[j].l_pixel = read_card32 (ifp, &err);
 
867
 
 
868
      read_card32 (ifp, &err);
 
869
      colormap[j].l_red   = read_card32 (ifp, &err);
 
870
      read_card32 (ifp, &err);
 
871
      colormap[j].l_green = read_card32 (ifp, &err);
 
872
      read_card32 (ifp, &err);
 
873
      colormap[j].l_blue  = read_card32 (ifp, &err);
 
874
 
 
875
      /* The flag byte is kept in the first byte */
 
876
      colormap[j].l_flags = read_card8 (ifp, &err);
 
877
      colormap[j].l_pad   = read_card8 (ifp, &err);
 
878
      read_card16 (ifp, &err);
 
879
      read_card32 (ifp, &err);
 
880
 
 
881
      if (indexed && (colormap[j].l_pixel > 255))
 
882
        index_is_bad++;
 
883
 
 
884
       if (err)
 
885
         break;
 
886
    }
 
887
 
 
888
  if (flag_is_bad || index_is_bad)
 
889
    {
 
890
      printf ("xwd: Warning. Error in XWD-color-structure (");
 
891
 
 
892
      if (flag_is_bad) printf ("flag");
 
893
      if (index_is_bad) printf ("index");
 
894
 
 
895
      printf (")\n");
 
896
    }
 
897
 
 
898
  if (err)
 
899
    xwdhdr->l_file_version = 0;  /* Not a valid XWD-file */
 
900
}
 
901
 
 
902
 
 
903
static void
 
904
write_xwd_cols (FILE            *ofp,
 
905
                L_XWDFILEHEADER *xwdhdr,
 
906
                L_XWDCOLOR      *colormap)
 
907
 
 
908
{
 
909
  int j;
 
910
 
 
911
  for (j = 0; j < xwdhdr->l_colormap_entries; j++)
 
912
    {
 
913
#ifdef CRAY
 
914
      write_card32 (ofp, (L_CARD32)0);
 
915
      write_card32 (ofp, colormap[j].l_pixel);
 
916
      write_card32 (ofp, (L_CARD32)0);
 
917
      write_card32 (ofp, (L_CARD32)colormap[j].l_red);
 
918
      write_card32 (ofp, (L_CARD32)0);
 
919
      write_card32 (ofp, (L_CARD32)colormap[j].l_green);
 
920
      write_card32 (ofp, (L_CARD32)0);
 
921
      write_card32 (ofp, (L_CARD32)colormap[j].l_blue);
 
922
      write_card8  (ofp, (L_CARD32)colormap[j].l_flags);
 
923
      write_card8  (ofp, (L_CARD32)colormap[j].l_pad);
 
924
      write_card16 (ofp, (L_CARD32)0);
 
925
      write_card32 (ofp, (L_CARD32)0);
 
926
#else
 
927
#ifdef __alpha
 
928
      write_card32 (ofp, colormap[j].l_pixel);
 
929
      write_card32 (ofp, (L_CARD32)0);
 
930
      write_card16 (ofp, (L_CARD32)colormap[j].l_red);
 
931
      write_card16 (ofp, (L_CARD32)colormap[j].l_green);
 
932
      write_card16 (ofp, (L_CARD32)colormap[j].l_blue);
 
933
      write_card8  (ofp, (L_CARD32)colormap[j].l_flags);
 
934
      write_card8  (ofp, (L_CARD32)colormap[j].l_pad);
 
935
#else
 
936
      write_card32 (ofp, colormap[j].l_pixel);
 
937
      write_card16 (ofp, (L_CARD32)colormap[j].l_red);
 
938
      write_card16 (ofp, (L_CARD32)colormap[j].l_green);
 
939
      write_card16 (ofp, (L_CARD32)colormap[j].l_blue);
 
940
      write_card8  (ofp, (L_CARD32)colormap[j].l_flags);
 
941
      write_card8  (ofp, (L_CARD32)colormap[j].l_pad);
 
942
#endif
 
943
#endif
 
944
    }
 
945
}
 
946
 
 
947
 
 
948
/* Create a map for mapping up to 32 bit pixelvalues to RGB. */
 
949
/* Returns number of colors kept in the map (up to 256) */
 
950
 
 
951
static gint
 
952
set_pixelmap (int         ncols,
 
953
              L_XWDCOLOR *xwdcol,
 
954
              PIXEL_MAP  *pixelmap)
 
955
 
 
956
{
 
957
  gint     i, j, k, maxcols;
 
958
  L_CARD32 pixel_val;
 
959
 
 
960
  memset ((gchar *) pixelmap, 0, sizeof (PIXEL_MAP));
 
961
 
 
962
  maxcols = 0;
 
963
 
 
964
  for (j = 0; j < ncols; j++) /* For each entry of the XWD colormap */
 
965
    {
 
966
      pixel_val = xwdcol[j].l_pixel;
 
967
      for (k = 0; k < maxcols; k++)  /* Where to insert in list ? */
 
968
        {
 
969
          if (pixel_val <= pixelmap->pmap[k].pixel_val)
 
970
            break;
 
971
        }
 
972
      if ((k < maxcols) && (pixel_val == pixelmap->pmap[k].pixel_val))
 
973
        break;   /* It was already in list */
 
974
 
 
975
      if (k >= 256)
 
976
        break;
 
977
 
 
978
      if (k < maxcols)   /* Must move entries to the back ? */
 
979
        {
 
980
          for (i = maxcols-1; i >= k; i--)
 
981
            memcpy ((char *)&(pixelmap->pmap[i+1]),(char *)&(pixelmap->pmap[i]),
 
982
                    sizeof (PMAP));
 
983
        }
 
984
      pixelmap->pmap[k].pixel_val = pixel_val;
 
985
      pixelmap->pmap[k].red = xwdcol[j].l_red >> 8;
 
986
      pixelmap->pmap[k].green = xwdcol[j].l_green >> 8;
 
987
      pixelmap->pmap[k].blue = xwdcol[j].l_blue >> 8;
 
988
      pixelmap->pixel_in_map[pixel_val & MAPPERMASK] = 1;
 
989
      maxcols++;
 
990
    }
 
991
  pixelmap->npixel = maxcols;
 
992
#ifdef XWD_COL_DEBUG
 
993
  printf ("Colors in pixelmap: %d\n",pixelmap->npixel);
 
994
  for (j=0; j<pixelmap->npixel; j++)
 
995
    printf ("Pixelvalue 0x%08lx, 0x%02x 0x%02x 0x%02x\n",
 
996
            pixelmap->pmap[j].pixel_val,
 
997
            pixelmap->pmap[j].red,pixelmap->pmap[j].green,
 
998
            pixelmap->pmap[j].blue);
 
999
  for (j=0; j<=MAPPERMASK; j++)
 
1000
    printf ("0x%08lx: %d\n",(long)j,pixelmap->pixel_in_map[j]);
 
1001
#endif
 
1002
 
 
1003
  return pixelmap->npixel;
 
1004
}
 
1005
 
 
1006
 
 
1007
/* Search a pixel value in the pixel map. Returns FALSE if the */
 
1008
/* pixelval was not found in map. Returns TRUE if found. */
 
1009
 
 
1010
static gboolean
 
1011
get_pixelmap (L_CARD32   pixelval,
 
1012
              PIXEL_MAP *pixelmap,
 
1013
              guchar    *red,
 
1014
              guchar    *green,
 
1015
              guchar    *blue)
 
1016
 
 
1017
{
 
1018
  register PMAP *low, *high, *middle;
 
1019
 
 
1020
  if (pixelmap->npixel == 0)
 
1021
    return FALSE;
 
1022
 
 
1023
  if (!(pixelmap->pixel_in_map[pixelval & MAPPERMASK]))
 
1024
    return FALSE;
 
1025
 
 
1026
  low =  &(pixelmap->pmap[0]);
 
1027
  high = &(pixelmap->pmap[pixelmap->npixel-1]);
 
1028
 
 
1029
  /* Do a binary search on the array */
 
1030
  while (low < high)
 
1031
    {
 
1032
      middle = low + ((high - low)/2);
 
1033
      if (pixelval <= middle->pixel_val)
 
1034
        high = middle;
 
1035
      else
 
1036
        low = middle+1;
 
1037
    }
 
1038
 
 
1039
  if (pixelval == low->pixel_val)
 
1040
    {
 
1041
      *red = low->red; *green = low->green; *blue = low->blue;
 
1042
      return TRUE;
 
1043
    }
 
1044
 
 
1045
  return FALSE;
 
1046
}
 
1047
 
 
1048
 
 
1049
static void
 
1050
set_bw_color_table (gint32 image_ID)
 
1051
{
 
1052
  static guchar BWColorMap[2*3] = { 255, 255, 255, 0, 0, 0 };
 
1053
 
 
1054
#ifdef XWD_COL_DEBUG
 
1055
  printf ("Set GIMP b/w-colortable:\n");
 
1056
#endif
 
1057
 
 
1058
  gimp_image_set_colormap (image_ID, BWColorMap, 2);
 
1059
}
 
1060
 
 
1061
 
 
1062
/* Initialize an 8-bit colortable from the mask-values of the XWD-header */
 
1063
static void
 
1064
init_color_table256 (L_XWDFILEHEADER *xwdhdr,
 
1065
                     guchar          *ColorMap)
 
1066
 
 
1067
{
 
1068
  gint i, j, k, cuind;
 
1069
  gint redshift, greenshift, blueshift;
 
1070
  gint maxred, maxgreen, maxblue;
 
1071
 
 
1072
  /* Assume: the bit masks for red/green/blue are grouped together
 
1073
   * Example: redmask = 0xe0, greenmask = 0x1c, bluemask = 0x03
 
1074
   * We need to know where to place the RGB-values (shifting)
 
1075
   * and the maximum value for each component.
 
1076
   */
 
1077
  redshift = greenshift = blueshift = 0;
 
1078
  if ((maxred = xwdhdr->l_red_mask) == 0)
 
1079
    return;
 
1080
 
 
1081
  /* Shift the redmask to the rightmost bit position to get
 
1082
   * maximum value for red.
 
1083
   */
 
1084
  while ((maxred & 1) == 0)
 
1085
    {
 
1086
      redshift++;
 
1087
      maxred >>= 1;
 
1088
    }
 
1089
 
 
1090
  if ((maxgreen = xwdhdr->l_green_mask) == 0)
 
1091
    return;
 
1092
 
 
1093
  while ((maxgreen & 1) == 0)
 
1094
    {
 
1095
      greenshift++;
 
1096
      maxgreen >>= 1;
 
1097
    }
 
1098
 
 
1099
  if ((maxblue = xwdhdr->l_blue_mask) == 0)
 
1100
    return;
 
1101
 
 
1102
  while ((maxblue & 1) == 0)
 
1103
    {
 
1104
      blueshift++;
 
1105
      maxblue >>= 1;
 
1106
    }
 
1107
 
 
1108
  memset ((gchar *) ColorMap, 0, 256 * 3);
 
1109
 
 
1110
  for (i = 0; i <= maxred; i++)
 
1111
    for (j = 0; j <= maxgreen; j++)
 
1112
      for (k = 0; k <= maxblue; k++)
 
1113
        {
 
1114
          cuind = (i << redshift) | (j << greenshift) | (k << blueshift);
 
1115
 
 
1116
          if (cuind < 256)
 
1117
            {
 
1118
              ColorMap[cuind*3]   = (i * 255)/maxred;
 
1119
              ColorMap[cuind*3+1] = (j * 255)/maxgreen;
 
1120
              ColorMap[cuind*3+2] = (k * 255)/maxblue;
 
1121
            }
 
1122
        }
 
1123
}
 
1124
 
 
1125
 
 
1126
static void
 
1127
set_color_table (gint32           image_ID,
 
1128
                 L_XWDFILEHEADER *xwdhdr,
 
1129
                 L_XWDCOLOR      *xwdcolmap)
 
1130
 
 
1131
{
 
1132
  gint   ncols, i, j;
 
1133
  guchar ColorMap[256 * 3];
 
1134
 
 
1135
  ncols = xwdhdr->l_colormap_entries;
 
1136
  if (xwdhdr->l_ncolors < ncols)
 
1137
    ncols = xwdhdr->l_ncolors;
 
1138
  if (ncols <= 0)
 
1139
    return;
 
1140
  if (ncols > 256)
 
1141
    ncols = 256;
 
1142
 
 
1143
  /* Initialize color table for all 256 entries from mask-values */
 
1144
  init_color_table256 (xwdhdr, ColorMap);
 
1145
 
 
1146
  for (j = 0; j < ncols; j++)
 
1147
    {
 
1148
      i = xwdcolmap[j].l_pixel;
 
1149
      if ((i >= 0) && (i < 256))
 
1150
        {
 
1151
          ColorMap[i*3] = (xwdcolmap[j].l_red) >> 8;
 
1152
          ColorMap[i*3+1] = (xwdcolmap[j].l_green) >> 8;
 
1153
          ColorMap[i*3+2] = (xwdcolmap[j].l_blue) >> 8;
 
1154
        }
 
1155
    }
 
1156
 
 
1157
#ifdef XWD_COL_DEBUG
 
1158
  printf ("Set GIMP colortable:\n");
 
1159
  for (j = 0; j < 256; j++)
 
1160
    printf ("%3d: 0x%02x 0x%02x 0x%02x\n", j,
 
1161
            ColorMap[j*3], ColorMap[j*3+1], ColorMap[j*3+2]);
 
1162
#endif
 
1163
 
 
1164
  gimp_image_set_colormap (image_ID, ColorMap, 256);
 
1165
}
 
1166
 
 
1167
 
 
1168
 
 
1169
 
 
1170
/* Create an image. Sets layer_ID, drawable and rgn. Returns image_ID */
 
1171
static gint32
 
1172
create_new_image (const gchar         *filename,
 
1173
                  guint                width,
 
1174
                  guint                height,
 
1175
                  GimpImageBaseType    type,
 
1176
                  gint32              *layer_ID,
 
1177
                  GimpDrawable       **drawable,
 
1178
                  GimpPixelRgn        *pixel_rgn)
 
1179
{
 
1180
  gint32        image_ID;
 
1181
  GimpImageType gdtype;
 
1182
 
 
1183
  switch (type)
 
1184
    {
 
1185
    case GIMP_GRAY:
 
1186
      gdtype = GIMP_GRAY_IMAGE;
 
1187
      break;
 
1188
    case GIMP_INDEXED:
 
1189
      gdtype = GIMP_INDEXED_IMAGE;
 
1190
      break;
 
1191
    case GIMP_RGB:
 
1192
      gdtype = GIMP_RGB_IMAGE;
 
1193
      break;
 
1194
    default:
 
1195
      g_warning ("Unknown image type");
 
1196
      return -1;
 
1197
    }
 
1198
 
 
1199
  image_ID = gimp_image_new (width, height, type);
 
1200
  gimp_image_set_filename (image_ID, filename);
 
1201
 
 
1202
  *layer_ID = gimp_layer_new (image_ID, "Background", width, height,
 
1203
                              gdtype, 100, GIMP_NORMAL_MODE);
 
1204
  gimp_image_add_layer (image_ID, *layer_ID, 0);
 
1205
 
 
1206
  *drawable = gimp_drawable_get (*layer_ID);
 
1207
  gimp_pixel_rgn_init (pixel_rgn, *drawable, 0, 0, (*drawable)->width,
 
1208
                       (*drawable)->height, TRUE, FALSE);
 
1209
 
 
1210
  return image_ID;
 
1211
}
 
1212
 
 
1213
 
 
1214
/* Load XWD with pixmap_format 2, pixmap_depth 1, bits_per_pixel 1 */
 
1215
 
 
1216
static gint32
 
1217
load_xwd_f2_d1_b1 (const gchar     *filename,
 
1218
                   FILE            *ifp,
 
1219
                   L_XWDFILEHEADER *xwdhdr,
 
1220
                   L_XWDCOLOR      *xwdcolmap)
 
1221
{
 
1222
  register int     pix8;
 
1223
  register guchar *dest, *src;
 
1224
  guchar           c1, c2, c3, c4;
 
1225
  gint             width, height, linepad, scan_lines, tile_height;
 
1226
  gint             i, j, ncols;
 
1227
  gchar           *temp;
 
1228
  guchar           bit2byte[256 * 8];
 
1229
  guchar          *data, *scanline;
 
1230
  gint             err = 0;
 
1231
  gint32           layer_ID, image_ID;
 
1232
  GimpPixelRgn     pixel_rgn;
 
1233
  GimpDrawable    *drawable;
 
1234
 
 
1235
#ifdef XWD_DEBUG
 
1236
  printf ("load_xwd_f2_d1_b1 (%s)\n", filename);
 
1237
#endif
 
1238
 
 
1239
  width  = xwdhdr->l_pixmap_width;
 
1240
  height = xwdhdr->l_pixmap_height;
 
1241
 
 
1242
  image_ID = create_new_image (filename, width, height, GIMP_INDEXED,
 
1243
                               &layer_ID, &drawable, &pixel_rgn);
 
1244
 
 
1245
  tile_height = gimp_tile_height ();
 
1246
  data = g_malloc (tile_height * width);
 
1247
 
 
1248
  scanline = g_new (guchar, xwdhdr->l_bytes_per_line + 8);
 
1249
 
 
1250
  ncols = xwdhdr->l_colormap_entries;
 
1251
  if (xwdhdr->l_ncolors < ncols)
 
1252
    ncols = xwdhdr->l_ncolors;
 
1253
 
 
1254
  if (ncols < 2)
 
1255
    set_bw_color_table (image_ID);
 
1256
  else
 
1257
    set_color_table (image_ID, xwdhdr, xwdcolmap);
 
1258
 
 
1259
  temp = (gchar *) bit2byte;
 
1260
 
 
1261
  /* Get an array for mapping 8 bits in a byte to 8 bytes */
 
1262
  if (!xwdhdr->l_bitmap_bit_order)
 
1263
    {
 
1264
      for (j = 0; j < 256; j++)
 
1265
        for (i = 0; i < 8; i++)
 
1266
          *(temp++) = ((j & (1 << i)) != 0);
 
1267
    }
 
1268
  else
 
1269
    {
 
1270
      for (j = 0; j < 256; j++)
 
1271
        for (i = 7; i >= 0; i--)
 
1272
          *(temp++) = ((j & (1 << i)) != 0);
 
1273
    }
 
1274
 
 
1275
  linepad = xwdhdr->l_bytes_per_line - (xwdhdr->l_pixmap_width+7)/8;
 
1276
  if (linepad < 0)
 
1277
    linepad = 0;
 
1278
 
 
1279
  dest = data;
 
1280
  scan_lines = 0;
 
1281
 
 
1282
  for (i = 0; i < height; i++)
 
1283
    {
 
1284
      if (fread (scanline, xwdhdr->l_bytes_per_line, 1, ifp) != 1)
 
1285
        {
 
1286
          err = 1;
 
1287
          break;
 
1288
        }
 
1289
 
 
1290
      /* Need to check byte order ? */
 
1291
      if (xwdhdr->l_bitmap_bit_order != xwdhdr->l_byte_order)
 
1292
        {
 
1293
          src = scanline;
 
1294
          switch (xwdhdr->l_bitmap_unit)
 
1295
            {
 
1296
            case 16:
 
1297
              j = xwdhdr->l_bytes_per_line;
 
1298
              while (j > 0)
 
1299
                {
 
1300
                  c1 = src[0]; c2 = src[1];
 
1301
                  *(src++) = c2; *(src++) = c1;
 
1302
                  j -= 2;
 
1303
                }
 
1304
              break;
 
1305
 
 
1306
            case 32:
 
1307
              j = xwdhdr->l_bytes_per_line;
 
1308
              while (j > 0)
 
1309
                {
 
1310
                  c1 = src[0]; c2 = src[1]; c3 = src[2]; c4 = src[3];
 
1311
                  *(src++) = c4; *(src++) = c3; *(src++) = c2; *(src++) = c1;
 
1312
                  j -= 4;
 
1313
                }
 
1314
              break;
 
1315
            }
 
1316
        }
 
1317
      src = scanline;
 
1318
      j = width;
 
1319
      while (j >= 8)
 
1320
        {
 
1321
          pix8 = *(src++);
 
1322
          memcpy (dest, bit2byte + pix8*8, 8);
 
1323
          dest += 8;
 
1324
          j -= 8;
 
1325
        }
 
1326
      if (j > 0)
 
1327
        {
 
1328
          pix8 = *(src++);
 
1329
          memcpy (dest, bit2byte + pix8*8, j);
 
1330
          dest += j;
 
1331
        }
 
1332
 
 
1333
      scan_lines++;
 
1334
 
 
1335
      if ((i % 20) == 0)
 
1336
        gimp_progress_update ((double)(i+1) / (double)height);
 
1337
 
 
1338
      if ((scan_lines == tile_height) || ((i+1) == height))
 
1339
        {
 
1340
          gimp_pixel_rgn_set_rect (&pixel_rgn, data, 0, i-scan_lines+1,
 
1341
                                   width, scan_lines);
 
1342
          scan_lines = 0;
 
1343
          dest = data;
 
1344
        }
 
1345
      if (err) break;
 
1346
    }
 
1347
 
 
1348
  g_free (data);
 
1349
  g_free (scanline);
 
1350
 
 
1351
  if (err)
 
1352
    g_message (_("EOF encountered on reading"));
 
1353
 
 
1354
  gimp_drawable_flush (drawable);
 
1355
 
 
1356
  return err ? -1 : image_ID;
 
1357
}
 
1358
 
 
1359
 
 
1360
/* Load XWD with pixmap_format 2, pixmap_depth 8, bits_per_pixel 8 */
 
1361
 
 
1362
static gint32
 
1363
load_xwd_f2_d8_b8 (const gchar     *filename,
 
1364
                   FILE            *ifp,
 
1365
                   L_XWDFILEHEADER *xwdhdr,
 
1366
                   L_XWDCOLOR      *xwdcolmap)
 
1367
{
 
1368
  gint          width, height, linepad, tile_height, scan_lines;
 
1369
  gint          i, j, ncols;
 
1370
  gint          grayscale;
 
1371
  guchar       *dest, *data;
 
1372
  gint          err = 0;
 
1373
  gint32        layer_ID, image_ID;
 
1374
  GimpPixelRgn  pixel_rgn;
 
1375
  GimpDrawable *drawable;
 
1376
 
 
1377
#ifdef XWD_DEBUG
 
1378
  printf ("load_xwd_f2_d8_b8 (%s)\n", filename);
 
1379
#endif
 
1380
 
 
1381
  width  = xwdhdr->l_pixmap_width;
 
1382
  height = xwdhdr->l_pixmap_height;
 
1383
 
 
1384
  /* This could also be a grayscale image. Check it */
 
1385
  grayscale = 0;
 
1386
  if ((xwdhdr->l_ncolors == 256) && (xwdhdr->l_colormap_entries == 256))
 
1387
    {
 
1388
      for (j = 0; j < 256; j++)
 
1389
        {
 
1390
          if ((xwdcolmap[j].l_pixel != j)
 
1391
              || ((xwdcolmap[j].l_red >> 8) != j)
 
1392
              || ((xwdcolmap[j].l_green >> 8) != j)
 
1393
              || ((xwdcolmap[j].l_blue >> 8) != j))
 
1394
            break;
 
1395
        }
 
1396
 
 
1397
      grayscale = (j == 256);
 
1398
    }
 
1399
 
 
1400
  image_ID = create_new_image (filename, width, height,
 
1401
                               grayscale ? GIMP_GRAY : GIMP_INDEXED,
 
1402
                               &layer_ID, &drawable, &pixel_rgn);
 
1403
 
 
1404
  tile_height = gimp_tile_height ();
 
1405
  data = g_malloc (tile_height * width);
 
1406
 
 
1407
  if (!grayscale)
 
1408
    {
 
1409
      ncols = xwdhdr->l_colormap_entries;
 
1410
      if (xwdhdr->l_ncolors < ncols) ncols = xwdhdr->l_ncolors;
 
1411
      if (ncols < 2)
 
1412
        set_bw_color_table (image_ID);
 
1413
      else
 
1414
        set_color_table (image_ID, xwdhdr, xwdcolmap);
 
1415
    }
 
1416
 
 
1417
  linepad = xwdhdr->l_bytes_per_line - xwdhdr->l_pixmap_width;
 
1418
  if (linepad < 0)
 
1419
    linepad = 0;
 
1420
 
 
1421
  dest = data;
 
1422
  scan_lines = 0;
 
1423
 
 
1424
  for (i = 0; i < height; i++)
 
1425
    {
 
1426
      if (fread (dest, 1, width, ifp) != width)
 
1427
        {
 
1428
          err = 1;
 
1429
          break;
 
1430
        }
 
1431
      dest += width;
 
1432
 
 
1433
      for (j = 0; j < linepad; j++)
 
1434
        getc (ifp);
 
1435
 
 
1436
      scan_lines++;
 
1437
 
 
1438
      if ((i % 20) == 0)
 
1439
        gimp_progress_update ((gdouble) (i + 1) / (gdouble) height);
 
1440
 
 
1441
      if ((scan_lines == tile_height) || ((i+1) == height))
 
1442
        {
 
1443
          gimp_pixel_rgn_set_rect (&pixel_rgn, data, 0, i-scan_lines+1,
 
1444
                                   width, scan_lines);
 
1445
          scan_lines = 0;
 
1446
          dest = data;
 
1447
        }
 
1448
    }
 
1449
 
 
1450
  g_free (data);
 
1451
 
 
1452
  if (err)
 
1453
    g_message (_("EOF encountered on reading"));
 
1454
 
 
1455
  gimp_drawable_flush (drawable);
 
1456
 
 
1457
  return err ? -1 : image_ID;
 
1458
}
 
1459
 
 
1460
 
 
1461
/* Load XWD with pixmap_format 2, pixmap_depth up to 16, bits_per_pixel 16 */
 
1462
 
 
1463
static gint32
 
1464
load_xwd_f2_d16_b16 (const gchar     *filename,
 
1465
                     FILE            *ifp,
 
1466
                     L_XWDFILEHEADER *xwdhdr,
 
1467
                     L_XWDCOLOR      *xwdcolmap)
 
1468
{
 
1469
  register guchar *dest, lsbyte_first;
 
1470
  gint             width, height, linepad, i, j, c0, c1, ncols;
 
1471
  gint             red, green, blue, redval, greenval, blueval;
 
1472
  gint             maxred, maxgreen, maxblue;
 
1473
  gint             tile_height, scan_lines;
 
1474
  gulong           redmask, greenmask, bluemask;
 
1475
  guint            redshift, greenshift, blueshift;
 
1476
  gulong           maxval;
 
1477
  guchar          *ColorMap, *cm, *data;
 
1478
  gint             err = 0;
 
1479
  gint32           layer_ID, image_ID;
 
1480
  GimpPixelRgn     pixel_rgn;
 
1481
  GimpDrawable    *drawable;
 
1482
 
 
1483
#ifdef XWD_DEBUG
 
1484
  printf ("load_xwd_f2_d16_b16 (%s)\n", filename);
 
1485
#endif
 
1486
 
 
1487
  width  = xwdhdr->l_pixmap_width;
 
1488
  height = xwdhdr->l_pixmap_height;
 
1489
 
 
1490
  image_ID = create_new_image (filename, width, height, GIMP_RGB,
 
1491
                               &layer_ID, &drawable, &pixel_rgn);
 
1492
 
 
1493
  tile_height = gimp_tile_height ();
 
1494
  data = g_malloc (tile_height * width * 3);
 
1495
 
 
1496
  /* Get memory for mapping 16 bit XWD-pixel to GIMP-RGB */
 
1497
  maxval = 0x10000 * 3;
 
1498
  ColorMap = g_new0 (guchar, maxval);
 
1499
 
 
1500
  redmask   = xwdhdr->l_red_mask;
 
1501
  greenmask = xwdhdr->l_green_mask;
 
1502
  bluemask  = xwdhdr->l_blue_mask;
 
1503
 
 
1504
  /* How to shift RGB to be right aligned ? */
 
1505
  /* (We rely on the the mask bits are grouped and not mixed) */
 
1506
  redshift = greenshift = blueshift = 0;
 
1507
 
 
1508
  while (((1 << redshift)   & redmask)   == 0) redshift++;
 
1509
  while (((1 << greenshift) & greenmask) == 0) greenshift++;
 
1510
  while (((1 << blueshift)  & bluemask)  == 0) blueshift++;
 
1511
 
 
1512
  /* The bits_per_rgb may not be correct. Use redmask instead */
 
1513
  maxred = 0; while (redmask >> (redshift + maxred)) maxred++;
 
1514
  maxred = (1 << maxred) - 1;
 
1515
 
 
1516
  maxgreen = 0; while (greenmask >> (greenshift + maxgreen)) maxgreen++;
 
1517
  maxgreen = (1 << maxgreen) - 1;
 
1518
 
 
1519
  maxblue = 0; while (bluemask >> (blueshift + maxblue)) maxblue++;
 
1520
  maxblue = (1 << maxblue) - 1;
 
1521
 
 
1522
  /* Built up the array to map XWD-pixel value to GIMP-RGB */
 
1523
  for (red = 0; red <= maxred; red++)
 
1524
    {
 
1525
      redval = (red * 255) / maxred;
 
1526
      for (green = 0; green <= maxgreen; green++)
 
1527
        {
 
1528
          greenval = (green * 255) / maxgreen;
 
1529
          for (blue = 0; blue <= maxblue; blue++)
 
1530
            {
 
1531
              blueval = (blue * 255) / maxblue;
 
1532
              cm = ColorMap + ((red << redshift) + (green << greenshift)
 
1533
                               + (blue << blueshift)) * 3;
 
1534
              *(cm++) = redval;
 
1535
              *(cm++) = greenval;
 
1536
              *cm = blueval;
 
1537
            }
 
1538
        }
 
1539
    }
 
1540
 
 
1541
  /* Now look what was written to the XWD-Colormap */
 
1542
 
 
1543
  ncols = xwdhdr->l_colormap_entries;
 
1544
  if (xwdhdr->l_ncolors < ncols)
 
1545
    ncols = xwdhdr->l_ncolors;
 
1546
 
 
1547
  for (j = 0; j < ncols; j++)
 
1548
    {
 
1549
      cm = ColorMap + xwdcolmap[j].l_pixel * 3;
 
1550
      *(cm++) = (xwdcolmap[j].l_red >> 8);
 
1551
      *(cm++) = (xwdcolmap[j].l_green >> 8);
 
1552
      *cm = (xwdcolmap[j].l_blue >> 8);
 
1553
    }
 
1554
 
 
1555
  /* What do we have to consume after a line has finished ? */
 
1556
  linepad =   xwdhdr->l_bytes_per_line
 
1557
    - (xwdhdr->l_pixmap_width*xwdhdr->l_bits_per_pixel)/8;
 
1558
  if (linepad < 0) linepad = 0;
 
1559
 
 
1560
  lsbyte_first = (xwdhdr->l_byte_order == 0);
 
1561
 
 
1562
  dest = data;
 
1563
  scan_lines = 0;
 
1564
 
 
1565
  for (i = 0; i < height; i++)
 
1566
    {
 
1567
      for (j = 0; j < width; j++)
 
1568
        {
 
1569
          c0 = getc (ifp);
 
1570
          c1 = getc (ifp);
 
1571
          if (c1 < 0)
 
1572
            {
 
1573
              err = 1;
 
1574
              break;
 
1575
            }
 
1576
 
 
1577
          if (lsbyte_first)
 
1578
            c0 = c0 | (c1 << 8);
 
1579
          else
 
1580
            c0 = (c0 << 8) | c1;
 
1581
 
 
1582
          cm = ColorMap + c0 * 3;
 
1583
          *(dest++) = *(cm++);
 
1584
          *(dest++) = *(cm++);
 
1585
          *(dest++) = *cm;
 
1586
        }
 
1587
 
 
1588
      if (err)
 
1589
        break;
 
1590
 
 
1591
      for (j = 0; j < linepad; j++)
 
1592
        getc (ifp);
 
1593
 
 
1594
      scan_lines++;
 
1595
 
 
1596
      if ((i % 20) == 0)
 
1597
        gimp_progress_update ((double)(i+1) / (double)height);
 
1598
 
 
1599
      if ((scan_lines == tile_height) || ((i+1) == height))
 
1600
        {
 
1601
          gimp_pixel_rgn_set_rect (&pixel_rgn, data, 0, i-scan_lines+1,
 
1602
                                   width, scan_lines);
 
1603
          scan_lines = 0;
 
1604
          dest = data;
 
1605
        }
 
1606
    }
 
1607
  g_free (data);
 
1608
  g_free (ColorMap);
 
1609
 
 
1610
  if (err)
 
1611
    g_message (_("EOF encountered on reading"));
 
1612
 
 
1613
  gimp_drawable_flush (drawable);
 
1614
 
 
1615
  return err ? -1 : image_ID;
 
1616
}
 
1617
 
 
1618
 
 
1619
/* Load XWD with pixmap_format 2, pixmap_depth up to 24, bits_per_pixel 24/32 */
 
1620
 
 
1621
static gint32
 
1622
load_xwd_f2_d24_b32 (const gchar      *filename,
 
1623
                     FILE             *ifp,
 
1624
                     L_XWDFILEHEADER  *xwdhdr,
 
1625
                     L_XWDCOLOR       *xwdcolmap,
 
1626
                     GError          **error)
 
1627
{
 
1628
  register guchar *dest, lsbyte_first;
 
1629
  gint             width, height, linepad, i, j, c0, c1, c2, c3;
 
1630
  gint             tile_height, scan_lines;
 
1631
  L_CARD32         pixelval;
 
1632
  gint             red, green, blue, ncols;
 
1633
  gint             maxred, maxgreen, maxblue;
 
1634
  gulong           redmask, greenmask, bluemask;
 
1635
  guint            redshift, greenshift, blueshift;
 
1636
  guchar           redmap[256], greenmap[256], bluemap[256];
 
1637
  guchar          *data;
 
1638
  PIXEL_MAP        pixel_map;
 
1639
  gint             err = 0;
 
1640
  gint32           layer_ID, image_ID;
 
1641
  GimpPixelRgn     pixel_rgn;
 
1642
  GimpDrawable    *drawable;
 
1643
 
 
1644
#ifdef XWD_DEBUG
 
1645
  printf ("load_xwd_f2_d24_b32 (%s)\n", filename);
 
1646
#endif
 
1647
 
 
1648
  width  = xwdhdr->l_pixmap_width;
 
1649
  height = xwdhdr->l_pixmap_height;
 
1650
 
 
1651
  redmask   = xwdhdr->l_red_mask;
 
1652
  greenmask = xwdhdr->l_green_mask;
 
1653
  bluemask  = xwdhdr->l_blue_mask;
 
1654
 
 
1655
  if (redmask   == 0) redmask   = 0xff0000;
 
1656
  if (greenmask == 0) greenmask = 0x00ff00;
 
1657
  if (bluemask  == 0) bluemask  = 0x0000ff;
 
1658
 
 
1659
  /* How to shift RGB to be right aligned ? */
 
1660
  /* (We rely on the the mask bits are grouped and not mixed) */
 
1661
  redshift = greenshift = blueshift = 0;
 
1662
 
 
1663
  while (((1 << redshift)   & redmask)   == 0) redshift++;
 
1664
  while (((1 << greenshift) & greenmask) == 0) greenshift++;
 
1665
  while (((1 << blueshift)  & bluemask)  == 0) blueshift++;
 
1666
 
 
1667
  /* The bits_per_rgb may not be correct. Use redmask instead */
 
1668
 
 
1669
  maxred = 0; while (redmask >> (redshift + maxred)) maxred++;
 
1670
  maxred = (1 << maxred) - 1;
 
1671
 
 
1672
  maxgreen = 0; while (greenmask >> (greenshift + maxgreen)) maxgreen++;
 
1673
  maxgreen = (1 << maxgreen) - 1;
 
1674
 
 
1675
  maxblue = 0; while (bluemask >> (blueshift + maxblue)) maxblue++;
 
1676
  maxblue = (1 << maxblue) - 1;
 
1677
 
 
1678
  if (maxred   > sizeof (redmap)   ||
 
1679
      maxgreen > sizeof (greenmap) ||
 
1680
      maxblue  > sizeof (bluemap))
 
1681
    {
 
1682
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
 
1683
                   _("XWD-file %s is corrupt."),
 
1684
                   gimp_filename_to_utf8 (filename));
 
1685
      return -1;
 
1686
    }
 
1687
 
 
1688
  image_ID = create_new_image (filename, width, height, GIMP_RGB,
 
1689
                               &layer_ID, &drawable, &pixel_rgn);
 
1690
 
 
1691
  tile_height = gimp_tile_height ();
 
1692
  data = g_malloc (tile_height * width * 3);
 
1693
 
 
1694
  /* Set map-arrays for red, green, blue */
 
1695
  for (red = 0; red <= maxred; red++)
 
1696
    redmap[red] = (red * 255) / maxred;
 
1697
  for (green = 0; green <= maxgreen; green++)
 
1698
    greenmap[green] = (green * 255) / maxgreen;
 
1699
  for (blue = 0; blue <= maxblue; blue++)
 
1700
    bluemap[blue] = (blue * 255) / maxblue;
 
1701
 
 
1702
  ncols = xwdhdr->l_colormap_entries;
 
1703
  if (xwdhdr->l_ncolors < ncols) ncols = xwdhdr->l_ncolors;
 
1704
 
 
1705
  ncols = set_pixelmap (ncols, xwdcolmap, &pixel_map);
 
1706
 
 
1707
  /* What do we have to consume after a line has finished ? */
 
1708
  linepad =   xwdhdr->l_bytes_per_line
 
1709
    - (xwdhdr->l_pixmap_width*xwdhdr->l_bits_per_pixel)/8;
 
1710
  if (linepad < 0) linepad = 0;
 
1711
 
 
1712
  lsbyte_first = (xwdhdr->l_byte_order == 0);
 
1713
 
 
1714
  dest = data;
 
1715
  scan_lines = 0;
 
1716
 
 
1717
  if (xwdhdr->l_bits_per_pixel == 32)
 
1718
    {
 
1719
      for (i = 0; i < height; i++)
 
1720
        {
 
1721
          for (j = 0; j < width; j++)
 
1722
            {
 
1723
              c0 = getc (ifp);
 
1724
              c1 = getc (ifp);
 
1725
              c2 = getc (ifp);
 
1726
              c3 = getc (ifp);
 
1727
              if (c3 < 0)
 
1728
                {
 
1729
                  err = 1;
 
1730
                  break;
 
1731
                }
 
1732
              if (lsbyte_first)
 
1733
                pixelval = c0 | (c1 << 8) | (c2 << 16) | (c3 << 24);
 
1734
              else
 
1735
                pixelval = (c0 << 24) | (c1 << 16) | (c2 << 8) | c3;
 
1736
 
 
1737
              if (get_pixelmap (pixelval, &pixel_map, dest, dest+1, dest+2))
 
1738
                {
 
1739
                  dest += 3;
 
1740
                }
 
1741
              else
 
1742
                {
 
1743
                  *(dest++) = redmap[(pixelval & redmask) >> redshift];
 
1744
                  *(dest++) = greenmap[(pixelval & greenmask) >> greenshift];
 
1745
                  *(dest++) = bluemap[(pixelval & bluemask) >> blueshift];
 
1746
                }
 
1747
            }
 
1748
          scan_lines++;
 
1749
 
 
1750
          if (err)
 
1751
            break;
 
1752
 
 
1753
          for (j = 0; j < linepad; j++)
 
1754
            getc (ifp);
 
1755
 
 
1756
          if ((i % 20) == 0)
 
1757
            gimp_progress_update ((gdouble) (i + 1) / (gdouble) height);
 
1758
 
 
1759
          if ((scan_lines == tile_height) || ((i+1) == height))
 
1760
            {
 
1761
              gimp_pixel_rgn_set_rect (&pixel_rgn, data, 0, i-scan_lines+1,
 
1762
                                       width, scan_lines);
 
1763
              scan_lines = 0;
 
1764
              dest = data;
 
1765
            }
 
1766
        }
 
1767
    }
 
1768
  else    /* 24 bits per pixel */
 
1769
    {
 
1770
      for (i = 0; i < height; i++)
 
1771
        {
 
1772
          for (j = 0; j < width; j++)
 
1773
            {
 
1774
              c0 = getc (ifp);
 
1775
              c1 = getc (ifp);
 
1776
              c2 = getc (ifp);
 
1777
              if (c2 < 0)
 
1778
                {
 
1779
                  err = 1;
 
1780
                  break;
 
1781
                }
 
1782
              if (lsbyte_first)
 
1783
                pixelval = c0 | (c1 << 8) | (c2 << 16);
 
1784
              else
 
1785
                pixelval = (c0 << 16) | (c1 << 8) | c2;
 
1786
 
 
1787
              if (get_pixelmap (pixelval, &pixel_map, dest, dest+1, dest+2))
 
1788
                {
 
1789
                  dest += 3;
 
1790
                }
 
1791
              else
 
1792
                {
 
1793
                  *(dest++) = redmap[(pixelval & redmask) >> redshift];
 
1794
                  *(dest++) = greenmap[(pixelval & greenmask) >> greenshift];
 
1795
                  *(dest++) = bluemap[(pixelval & bluemask) >> blueshift];
 
1796
                }
 
1797
            }
 
1798
          scan_lines++;
 
1799
 
 
1800
          if (err)
 
1801
            break;
 
1802
 
 
1803
          for (j = 0; j < linepad; j++)
 
1804
            getc (ifp);
 
1805
 
 
1806
          if ((i % 20) == 0)
 
1807
            gimp_progress_update ((gdouble) (i + 1) / (gdouble) height);
 
1808
 
 
1809
          if ((scan_lines == tile_height) || ((i+1) == height))
 
1810
            {
 
1811
              gimp_pixel_rgn_set_rect (&pixel_rgn, data, 0, i-scan_lines+1,
 
1812
                                       width, scan_lines);
 
1813
              scan_lines = 0;
 
1814
              dest = data;
 
1815
            }
 
1816
        }
 
1817
    }
 
1818
 
 
1819
  g_free (data);
 
1820
 
 
1821
  if (err)
 
1822
    g_message (_("EOF encountered on reading"));
 
1823
 
 
1824
  gimp_drawable_flush (drawable);
 
1825
 
 
1826
  return err ? -1 : image_ID;
 
1827
}
 
1828
 
 
1829
 
 
1830
/* Load XWD with pixmap_format 1, pixmap_depth up to 24, bits_per_pixel 1 */
 
1831
 
 
1832
static gint32
 
1833
load_xwd_f1_d24_b1 (const gchar      *filename,
 
1834
                    FILE             *ifp,
 
1835
                    L_XWDFILEHEADER  *xwdhdr,
 
1836
                    L_XWDCOLOR       *xwdcolmap,
 
1837
                    GError          **error)
 
1838
{
 
1839
  register guchar *dest, outmask, inmask, do_reverse;
 
1840
  gint             width, height, linepad, i, j, plane, fromright;
 
1841
  gint             tile_height, tile_start, tile_end;
 
1842
  gint             indexed, bytes_per_pixel;
 
1843
  gint             maxred, maxgreen, maxblue;
 
1844
  gint             red, green, blue, ncols, standard_rgb;
 
1845
  glong            data_offset, plane_offset, tile_offset;
 
1846
  gulong           redmask, greenmask, bluemask;
 
1847
  guint            redshift, greenshift, blueshift;
 
1848
  gulong           g;
 
1849
  guchar           redmap[256], greenmap[256], bluemap[256];
 
1850
  guchar           bit_reverse[256];
 
1851
  guchar          *xwddata, *xwdin, *data;
 
1852
  L_CARD32         pixelval;
 
1853
  PIXEL_MAP        pixel_map;
 
1854
  gint             err = 0;
 
1855
  gint32           layer_ID, image_ID;
 
1856
  GimpPixelRgn     pixel_rgn;
 
1857
  GimpDrawable    *drawable;
 
1858
 
 
1859
#ifdef XWD_DEBUG
 
1860
  printf ("load_xwd_f1_d24_b1 (%s)\n", filename);
 
1861
#endif
 
1862
 
 
1863
  xwddata = g_malloc (xwdhdr->l_bytes_per_line);
 
1864
  if (xwddata == NULL)
 
1865
    return -1;
 
1866
 
 
1867
  width           = xwdhdr->l_pixmap_width;
 
1868
  height          = xwdhdr->l_pixmap_height;
 
1869
  indexed         = (xwdhdr->l_pixmap_depth <= 8);
 
1870
  bytes_per_pixel = (indexed ? 1 : 3);
 
1871
 
 
1872
  linepad =   xwdhdr->l_bytes_per_line
 
1873
    - (xwdhdr->l_pixmap_width+7)/8;
 
1874
  if (linepad < 0)
 
1875
    linepad = 0;
 
1876
 
 
1877
  for (j = 0; j < 256; j++)   /* Create an array for reversing bits */
 
1878
    {
 
1879
      inmask = 0;
 
1880
      for (i = 0; i < 8; i++)
 
1881
        {
 
1882
          inmask <<= 1;
 
1883
          if (j & (1 << i)) inmask |= 1;
 
1884
        }
 
1885
      bit_reverse[j] = inmask;
 
1886
    }
 
1887
 
 
1888
  redmask   = xwdhdr->l_red_mask;
 
1889
  greenmask = xwdhdr->l_green_mask;
 
1890
  bluemask  = xwdhdr->l_blue_mask;
 
1891
 
 
1892
  if (redmask   == 0) redmask   = 0xff0000;
 
1893
  if (greenmask == 0) greenmask = 0x00ff00;
 
1894
  if (bluemask  == 0) bluemask  = 0x0000ff;
 
1895
 
 
1896
  standard_rgb =    (redmask == 0xff0000) && (greenmask == 0x00ff00)
 
1897
    && (bluemask == 0x0000ff);
 
1898
  redshift = greenshift = blueshift = 0;
 
1899
 
 
1900
  if (!standard_rgb)   /* Do we need to re-map the pixel-values ? */
 
1901
    {
 
1902
      /* How to shift RGB to be right aligned ? */
 
1903
      /* (We rely on the the mask bits are grouped and not mixed) */
 
1904
 
 
1905
      while (((1 << redshift)   & redmask)   == 0) redshift++;
 
1906
      while (((1 << greenshift) & greenmask) == 0) greenshift++;
 
1907
      while (((1 << blueshift)  & bluemask)  == 0) blueshift++;
 
1908
 
 
1909
      /* The bits_per_rgb may not be correct. Use redmask instead */
 
1910
 
 
1911
      maxred = 0; while (redmask >> (redshift + maxred)) maxred++;
 
1912
      maxred = (1 << maxred) - 1;
 
1913
 
 
1914
      maxgreen = 0; while (greenmask >> (greenshift + maxgreen)) maxgreen++;
 
1915
      maxgreen = (1 << maxgreen) - 1;
 
1916
 
 
1917
      maxblue = 0; while (bluemask >> (blueshift + maxblue)) maxblue++;
 
1918
      maxblue = (1 << maxblue) - 1;
 
1919
 
 
1920
      if (maxred   > sizeof (redmap)   ||
 
1921
          maxgreen > sizeof (greenmap) ||
 
1922
          maxblue  > sizeof (bluemap))
 
1923
        {
 
1924
          g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
 
1925
                       _("XWD-file %s is corrupt."),
 
1926
                       gimp_filename_to_utf8 (filename));
 
1927
          return -1;
 
1928
        }
 
1929
 
 
1930
      /* Set map-arrays for red, green, blue */
 
1931
      for (red = 0; red <= maxred; red++)
 
1932
        redmap[red] = (red * 255) / maxred;
 
1933
      for (green = 0; green <= maxgreen; green++)
 
1934
        greenmap[green] = (green * 255) / maxgreen;
 
1935
      for (blue = 0; blue <= maxblue; blue++)
 
1936
        bluemap[blue] = (blue * 255) / maxblue;
 
1937
    }
 
1938
 
 
1939
  image_ID = create_new_image (filename, width, height,
 
1940
                               indexed ? GIMP_INDEXED : GIMP_RGB,
 
1941
                               &layer_ID, &drawable, &pixel_rgn);
 
1942
 
 
1943
  tile_height = gimp_tile_height ();
 
1944
  data = g_malloc (tile_height * width * bytes_per_pixel);
 
1945
 
 
1946
  ncols = xwdhdr->l_colormap_entries;
 
1947
  if (xwdhdr->l_ncolors < ncols)
 
1948
    ncols = xwdhdr->l_ncolors;
 
1949
 
 
1950
  if (indexed)
 
1951
    {
 
1952
      if (ncols < 2)
 
1953
        set_bw_color_table (image_ID);
 
1954
      else
 
1955
        set_color_table (image_ID, xwdhdr, xwdcolmap);
 
1956
    }
 
1957
  else
 
1958
    {
 
1959
      ncols = set_pixelmap (ncols, xwdcolmap, &pixel_map);
 
1960
    }
 
1961
 
 
1962
  do_reverse = !xwdhdr->l_bitmap_bit_order;
 
1963
 
 
1964
  /* This is where the image data starts within the file */
 
1965
  data_offset = ftell (ifp);
 
1966
 
 
1967
  for (tile_start = 0; tile_start < height; tile_start += tile_height)
 
1968
    {
 
1969
      memset (data, 0, width*tile_height*bytes_per_pixel);
 
1970
 
 
1971
      tile_end = tile_start + tile_height - 1;
 
1972
      if (tile_end >= height)
 
1973
        tile_end = height - 1;
 
1974
 
 
1975
      for (plane = 0; plane < xwdhdr->l_pixmap_depth; plane++)
 
1976
        {
 
1977
          dest = data;    /* Position to start of tile within the plane */
 
1978
          plane_offset = data_offset + plane*height*xwdhdr->l_bytes_per_line;
 
1979
          tile_offset = plane_offset + tile_start*xwdhdr->l_bytes_per_line;
 
1980
          fseek (ifp, tile_offset, SEEK_SET);
 
1981
 
 
1982
          /* Place the last plane at the least significant bit */
 
1983
 
 
1984
          if (indexed)   /* Only 1 byte per pixel */
 
1985
            {
 
1986
              fromright = xwdhdr->l_pixmap_depth-1-plane;
 
1987
              outmask = (1 << fromright);
 
1988
            }
 
1989
          else           /* 3 bytes per pixel */
 
1990
            {
 
1991
              fromright = xwdhdr->l_pixmap_depth-1-plane;
 
1992
              dest += 2 - fromright/8;
 
1993
              outmask = (1 << (fromright % 8));
 
1994
            }
 
1995
 
 
1996
          for (i = tile_start; i <= tile_end; i++)
 
1997
            {
 
1998
              if (fread (xwddata,xwdhdr->l_bytes_per_line,1,ifp) != 1)
 
1999
                {
 
2000
                  err = 1;
 
2001
                  break;
 
2002
                }
 
2003
              xwdin = xwddata;
 
2004
 
 
2005
              /* Handle bitmap unit */
 
2006
              if (xwdhdr->l_bitmap_unit == 16)
 
2007
                {
 
2008
                  if (xwdhdr->l_bitmap_bit_order != xwdhdr->l_byte_order)
 
2009
                    {
 
2010
                      j = xwdhdr->l_bytes_per_line/2;
 
2011
                      while (j--)
 
2012
                        {
 
2013
                          inmask = xwdin[0]; xwdin[0] = xwdin[1]; xwdin[1] = inmask;
 
2014
                          xwdin += 2;
 
2015
                        }
 
2016
                      xwdin = xwddata;
 
2017
                    }
 
2018
                }
 
2019
              else if (xwdhdr->l_bitmap_unit == 32)
 
2020
                {
 
2021
                  if (xwdhdr->l_bitmap_bit_order != xwdhdr->l_byte_order)
 
2022
                    {
 
2023
                      j = xwdhdr->l_bytes_per_line/4;
 
2024
                      while (j--)
 
2025
                        {
 
2026
                          inmask = xwdin[0]; xwdin[0] = xwdin[3]; xwdin[3] = inmask;
 
2027
                          inmask = xwdin[1]; xwdin[1] = xwdin[2]; xwdin[2] = inmask;
 
2028
                          xwdin += 4;
 
2029
                        }
 
2030
                      xwdin = xwddata;
 
2031
                    }
 
2032
                }
 
2033
 
 
2034
              g = inmask = 0;
 
2035
              for (j = 0; j < width; j++)
 
2036
                {
 
2037
                  if (!inmask)
 
2038
                    {
 
2039
                      g = *(xwdin++);
 
2040
                      if (do_reverse)
 
2041
                        g = bit_reverse[g];
 
2042
                      inmask = 0x80;
 
2043
                    }
 
2044
 
 
2045
                  if (g & inmask)
 
2046
                    *dest |= outmask;
 
2047
                  dest += bytes_per_pixel;
 
2048
 
 
2049
                  inmask >>= 1;
 
2050
                }
 
2051
            }
 
2052
        }
 
2053
 
 
2054
      /* For indexed images, the mapping to colors is done by the color table. */
 
2055
      /* Otherwise we must do the mapping by ourself. */
 
2056
      if (!indexed)
 
2057
        {
 
2058
          dest = data;
 
2059
          for (i = tile_start; i <= tile_end; i++)
 
2060
            {
 
2061
              for (j = 0; j < width; j++)
 
2062
                {
 
2063
                  pixelval = (*dest << 16) | (*(dest+1) << 8) | *(dest+2);
 
2064
 
 
2065
                  if (get_pixelmap (pixelval, &pixel_map, dest, dest+1, dest+2)
 
2066
                      || standard_rgb)
 
2067
                    {
 
2068
                      dest += 3;
 
2069
                    }
 
2070
                  else   /* We have to map RGB to 0,...,255 */
 
2071
                    {
 
2072
                      *(dest++) = redmap[(pixelval & redmask) >> redshift];
 
2073
                      *(dest++) = greenmap[(pixelval & greenmask) >> greenshift];
 
2074
                      *(dest++) = bluemap[(pixelval & bluemask) >> blueshift];
 
2075
                    }
 
2076
                }
 
2077
            }
 
2078
        }
 
2079
 
 
2080
      gimp_progress_update ((gdouble) tile_end / (gdouble) height);
 
2081
 
 
2082
      gimp_pixel_rgn_set_rect (&pixel_rgn, data, 0, tile_start,
 
2083
                               width, tile_end-tile_start+1);
 
2084
    }
 
2085
 
 
2086
  g_free (data);
 
2087
  g_free (xwddata);
 
2088
 
 
2089
  if (err)
 
2090
    g_message (_("EOF encountered on reading"));
 
2091
 
 
2092
  gimp_drawable_flush (drawable);
 
2093
 
 
2094
  return err ? -1 : image_ID;
 
2095
}
 
2096
 
 
2097
 
 
2098
static gint
 
2099
save_index (FILE    *ofp,
 
2100
            gint32   image_ID,
 
2101
            gint32   drawable_ID,
 
2102
            gint     gray)
 
2103
{
 
2104
  gint             height, width, linepad, tile_height, i, j;
 
2105
  gint             ncolors, vclass;
 
2106
  glong            tmp = 0;
 
2107
  guchar          *data, *src, *cmap;
 
2108
  L_XWDFILEHEADER  xwdhdr;
 
2109
  L_XWDCOLOR       xwdcolmap[256];
 
2110
  GimpPixelRgn     pixel_rgn;
 
2111
  GimpDrawable    *drawable;
 
2112
  GimpImageType    drawable_type;
 
2113
 
 
2114
#ifdef XWD_DEBUG
 
2115
  printf ("save_index ()\n");
 
2116
#endif
 
2117
 
 
2118
  drawable      = gimp_drawable_get (drawable_ID);
 
2119
  drawable_type = gimp_drawable_type (drawable_ID);
 
2120
  width         = drawable->width;
 
2121
  height        = drawable->height;
 
2122
  tile_height   = gimp_tile_height ();
 
2123
 
 
2124
  gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0, width, height, FALSE, FALSE);
 
2125
 
 
2126
  /* allocate a buffer for retrieving information from the pixel region  */
 
2127
  src = data = g_new (guchar, tile_height * width * drawable->bpp);
 
2128
 
 
2129
  linepad = width % 4;
 
2130
  if (linepad) linepad = 4 - linepad;
 
2131
 
 
2132
  /* Fill XWD-color map */
 
2133
  if (gray)
 
2134
    {
 
2135
      vclass = 0;
 
2136
      ncolors = 256;
 
2137
 
 
2138
      for (j = 0; j < ncolors; j++)
 
2139
        {
 
2140
          xwdcolmap[j].l_pixel = j;
 
2141
          xwdcolmap[j].l_red   = (j << 8) | j;
 
2142
          xwdcolmap[j].l_green = (j << 8) | j;
 
2143
          xwdcolmap[j].l_blue  = (j << 8) | j;
 
2144
          xwdcolmap[j].l_flags = 7;
 
2145
          xwdcolmap[j].l_pad = 0;
 
2146
        }
 
2147
    }
 
2148
  else
 
2149
    {
 
2150
      vclass = 3;
 
2151
      cmap = gimp_image_get_colormap (image_ID, &ncolors);
 
2152
 
 
2153
      for (j = 0; j < ncolors; j++)
 
2154
        {
 
2155
          xwdcolmap[j].l_pixel = j;
 
2156
          xwdcolmap[j].l_red   = ((*cmap) << 8) | *cmap; cmap++;
 
2157
          xwdcolmap[j].l_green = ((*cmap) << 8) | *cmap; cmap++;
 
2158
          xwdcolmap[j].l_blue  = ((*cmap) << 8) | *cmap; cmap++;
 
2159
          xwdcolmap[j].l_flags = 7;
 
2160
          xwdcolmap[j].l_pad = 0;
 
2161
        }
 
2162
    }
 
2163
 
 
2164
  /* Fill in the XWD header (header_size is evaluated by write_xwd_hdr ()) */
 
2165
  xwdhdr.l_header_size      = 0;
 
2166
  xwdhdr.l_file_version     = 7;
 
2167
  xwdhdr.l_pixmap_format    = 2;
 
2168
  xwdhdr.l_pixmap_depth     = 8;
 
2169
  xwdhdr.l_pixmap_width     = width;
 
2170
  xwdhdr.l_pixmap_height    = height;
 
2171
  xwdhdr.l_xoffset          = 0;
 
2172
  xwdhdr.l_byte_order       = 1;
 
2173
  xwdhdr.l_bitmap_unit      = 32;
 
2174
  xwdhdr.l_bitmap_bit_order = 1;
 
2175
  xwdhdr.l_bitmap_pad       = 32;
 
2176
  xwdhdr.l_bits_per_pixel   = 8;
 
2177
  xwdhdr.l_bytes_per_line   = width + linepad;
 
2178
  xwdhdr.l_visual_class     = vclass;
 
2179
  xwdhdr.l_red_mask         = 0x000000;
 
2180
  xwdhdr.l_green_mask       = 0x000000;
 
2181
  xwdhdr.l_blue_mask        = 0x000000;
 
2182
  xwdhdr.l_bits_per_rgb     = 8;
 
2183
  xwdhdr.l_colormap_entries = ncolors;
 
2184
  xwdhdr.l_ncolors          = ncolors;
 
2185
  xwdhdr.l_window_width     = width;
 
2186
  xwdhdr.l_window_height    = height;
 
2187
  xwdhdr.l_window_x         = 64;
 
2188
  xwdhdr.l_window_y         = 64;
 
2189
  xwdhdr.l_window_bdrwidth  = 0;
 
2190
 
 
2191
  write_xwd_header (ofp, &xwdhdr);
 
2192
  write_xwd_cols (ofp, &xwdhdr, xwdcolmap);
 
2193
 
 
2194
  for (i = 0; i < height; i++)
 
2195
    {
 
2196
      if ((i % tile_height) == 0)   /* Get more data */
 
2197
        {
 
2198
          gint scan_lines = (i + tile_height - 1 < height) ? tile_height : (height - i);
 
2199
 
 
2200
          gimp_pixel_rgn_get_rect (&pixel_rgn, data, 0, i, width, scan_lines);
 
2201
          src = data;
 
2202
        }
 
2203
 
 
2204
      fwrite (src, width, 1, ofp);
 
2205
 
 
2206
      if (linepad)
 
2207
        fwrite ((char *)&tmp, linepad, 1, ofp);
 
2208
 
 
2209
      src += width;
 
2210
 
 
2211
      if ((i % 20) == 0)
 
2212
        gimp_progress_update ((gdouble) i / (gdouble) height);
 
2213
    }
 
2214
 
 
2215
  g_free (data);
 
2216
 
 
2217
  gimp_drawable_detach (drawable);
 
2218
 
 
2219
  if (ferror (ofp))
 
2220
    {
 
2221
      g_message (_("Error during writing indexed/gray image"));
 
2222
      return FALSE;
 
2223
    }
 
2224
 
 
2225
  return TRUE;
 
2226
}
 
2227
 
 
2228
 
 
2229
static gint
 
2230
save_rgb (FILE   *ofp,
 
2231
          gint32  image_ID,
 
2232
          gint32  drawable_ID)
 
2233
{
 
2234
  gint             height, width, linepad, tile_height, i;
 
2235
  glong            tmp = 0;
 
2236
  guchar          *data, *src;
 
2237
  L_XWDFILEHEADER  xwdhdr;
 
2238
  GimpPixelRgn     pixel_rgn;
 
2239
  GimpDrawable    *drawable;
 
2240
  GimpImageType    drawable_type;
 
2241
 
 
2242
#ifdef XWD_DEBUG
 
2243
  printf ("save_rgb ()\n");
 
2244
#endif
 
2245
 
 
2246
  drawable      = gimp_drawable_get (drawable_ID);
 
2247
  drawable_type = gimp_drawable_type (drawable_ID);
 
2248
  width         = drawable->width;
 
2249
  height        = drawable->height;
 
2250
  tile_height   = gimp_tile_height ();
 
2251
 
 
2252
  gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0, width, height, FALSE, FALSE);
 
2253
 
 
2254
  /* allocate a buffer for retrieving information from the pixel region  */
 
2255
  src = data = g_new (guchar, tile_height * width * drawable->bpp);
 
2256
 
 
2257
  linepad = (width * 3) % 4;
 
2258
  if (linepad)
 
2259
    linepad = 4 - linepad;
 
2260
 
 
2261
  /* Fill in the XWD header (header_size is evaluated by write_xwd_hdr ()) */
 
2262
  xwdhdr.l_header_size      = 0;
 
2263
  xwdhdr.l_file_version     = 7;
 
2264
  xwdhdr.l_pixmap_format    = 2;
 
2265
  xwdhdr.l_pixmap_depth     = 24;
 
2266
  xwdhdr.l_pixmap_width     = width;
 
2267
  xwdhdr.l_pixmap_height    = height;
 
2268
  xwdhdr.l_xoffset          = 0;
 
2269
  xwdhdr.l_byte_order       = 1;
 
2270
 
 
2271
  xwdhdr.l_bitmap_unit      = 32;
 
2272
  xwdhdr.l_bitmap_bit_order = 1;
 
2273
  xwdhdr.l_bitmap_pad       = 32;
 
2274
  xwdhdr.l_bits_per_pixel   = 24;
 
2275
 
 
2276
  xwdhdr.l_bytes_per_line   = width * 3 + linepad;
 
2277
  xwdhdr.l_visual_class     = 5;
 
2278
  xwdhdr.l_red_mask         = 0xff0000;
 
2279
  xwdhdr.l_green_mask       = 0x00ff00;
 
2280
  xwdhdr.l_blue_mask        = 0x0000ff;
 
2281
  xwdhdr.l_bits_per_rgb     = 8;
 
2282
  xwdhdr.l_colormap_entries = 0;
 
2283
  xwdhdr.l_ncolors          = 0;
 
2284
  xwdhdr.l_window_width     = width;
 
2285
  xwdhdr.l_window_height    = height;
 
2286
  xwdhdr.l_window_x         = 64;
 
2287
  xwdhdr.l_window_y         = 64;
 
2288
  xwdhdr.l_window_bdrwidth  = 0;
 
2289
 
 
2290
  write_xwd_header (ofp, &xwdhdr);
 
2291
 
 
2292
  for (i = 0; i < height; i++)
 
2293
    {
 
2294
      if ((i % tile_height) == 0)   /* Get more data */
 
2295
        {
 
2296
          gint scan_lines = (i + tile_height - 1 < height) ? tile_height : (height - i);
 
2297
 
 
2298
          gimp_pixel_rgn_get_rect (&pixel_rgn, data, 0, i, width, scan_lines);
 
2299
          src = data;
 
2300
        }
 
2301
 
 
2302
      fwrite (src, width * 3, 1, ofp);
 
2303
 
 
2304
      if (linepad)
 
2305
        fwrite ((char *)&tmp, linepad, 1, ofp);
 
2306
 
 
2307
      src += width * 3;
 
2308
 
 
2309
      if ((i % 20) == 0)
 
2310
        gimp_progress_update ((gdouble) i / (gdouble) height);
 
2311
    }
 
2312
 
 
2313
  g_free (data);
 
2314
 
 
2315
  gimp_drawable_detach (drawable);
 
2316
 
 
2317
  if (ferror (ofp))
 
2318
    {
 
2319
      g_message (_("Error during writing rgb image"));
 
2320
      return FALSE;
 
2321
    }
 
2322
 
 
2323
  return TRUE;
 
2324
}