~ubuntu-branches/ubuntu/raring/gimp/raring-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:26:35 UTC
  • Revision ID: package-import@ubuntu.com-20131206132635-qpa5qocyku0xtpey
Tags: 2.8.4-1ubuntu1.1
* 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 3 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, see <http://www.gnu.org/licenses/>.
 
18
 *
 
19
 */
 
20
 
 
21
/*
 
22
 * XWD-input/output was written by Peter Kirchgessner (peter@kirchgessner.net)
 
23
 * Examples from mainly used UNIX-systems have been used for testing.
 
24
 * If a file does not work, please return a small (!!) compressed example.
 
25
 * Currently the following formats are supported:
 
26
 *    pixmap_format | pixmap_depth | bits_per_pixel
 
27
 *    ---------------------------------------------
 
28
 *          0       |        1     |       1
 
29
 *          1       |    1,...,24  |       1
 
30
 *          2       |        1     |       1
 
31
 *          2       |    1,...,8   |       8
 
32
 *          2       |    1,...,16  |      16
 
33
 *          2       |    1,...,24  |      24
 
34
 *          2       |    1,...,24  |      32
 
35
 */
 
36
/* Event history:
 
37
 * PK = Peter Kirchgessner, ME = Mattias Engdeg�rd
 
38
 * V 1.00, PK, xx-Aug-96: First try
 
39
 * V 1.01, PK, 03-Sep-96: Check for bitmap_bit_order
 
40
 * V 1.90, PK, 17-Mar-97: Upgrade to work with GIMP V0.99
 
41
 *                        Use visual class 3 to write indexed image
 
42
 *                        Set gimp b/w-colormap if no xwdcolormap present
 
43
 * V 1.91, PK, 05-Apr-97: Return all arguments, even in case of an error
 
44
 * V 1.92, PK, 12-Oct-97: No progress bars for non-interactive mode
 
45
 * V 1.93, PK, 11-Apr-98: Fix problem with overwriting memory
 
46
 * V 1.94, ME, 27-Feb-00: Remove superfluous little-endian support (format is
 
47
                          specified as big-endian). Trim magic header
 
48
 * V 1.95, PK, 02-Jul-01: Fix problem with 8 bit image
 
49
 */
 
50
 
 
51
#include "config.h"
 
52
 
 
53
#include <errno.h>
 
54
#include <string.h>
 
55
 
 
56
#include <glib/gstdio.h>
 
57
 
 
58
#include <libgimp/gimp.h>
 
59
#include <libgimp/gimpui.h>
 
60
 
 
61
#include "libgimp/stdplugins-intl.h"
 
62
 
 
63
 
 
64
#define LOAD_PROC      "file-xwd-load"
 
65
#define SAVE_PROC      "file-xwd-save"
 
66
#define PLUG_IN_BINARY "file-xwd"
 
67
#define PLUG_IN_ROLE   "gimp-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",     "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
 
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",     "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
 
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, NULL,
 
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
      g_free (xwdcolmap);
 
494
      fclose (ifp);
 
495
      return (-1);
 
496
    }
 
497
 
 
498
  if (xwdhdr.l_pixmap_width > GIMP_MAX_IMAGE_SIZE
 
499
      || xwdhdr.l_bytes_per_line > GIMP_MAX_IMAGE_SIZE * 3)
 
500
    {
 
501
      g_message (_("'%s':\nImage width is larger than GIMP can handle"),
 
502
                 gimp_filename_to_utf8 (filename));
 
503
      g_free (xwdcolmap);
 
504
      fclose (ifp);
 
505
      return (-1);
 
506
    }
 
507
 
 
508
  if (xwdhdr.l_pixmap_height <= 0)
 
509
    {
 
510
      g_message (_("'%s':\nNo image height specified"),
 
511
                 gimp_filename_to_utf8 (filename));
 
512
      g_free (xwdcolmap);
 
513
      fclose (ifp);
 
514
      return (-1);
 
515
    }
 
516
 
 
517
  if (xwdhdr.l_pixmap_height > GIMP_MAX_IMAGE_SIZE)
 
518
    {
 
519
      g_message (_("'%s':\nImage height is larger than GIMP can handle"),
 
520
                 gimp_filename_to_utf8 (filename));
 
521
      g_free (xwdcolmap);
 
522
      fclose (ifp);
 
523
      return (-1);
 
524
    }
 
525
 
 
526
  gimp_progress_init_printf (_("Opening '%s'"),
 
527
                             gimp_filename_to_utf8 (filename));
 
528
 
 
529
  depth = xwdhdr.l_pixmap_depth;
 
530
  bpp   = xwdhdr.l_bits_per_pixel;
 
531
 
 
532
  image_ID = -1;
 
533
  switch (xwdhdr.l_pixmap_format)
 
534
    {
 
535
    case 0:    /* Single plane bitmap */
 
536
      if ((depth == 1) && (bpp == 1))
 
537
        { /* Can be performed by format 2 loader */
 
538
          image_ID = load_xwd_f2_d1_b1 (filename, ifp, &xwdhdr, xwdcolmap);
 
539
        }
 
540
      break;
 
541
 
 
542
    case 1:    /* Single plane pixmap */
 
543
      if ((depth <= 24) && (bpp == 1))
 
544
        {
 
545
          image_ID = load_xwd_f1_d24_b1 (filename, ifp, &xwdhdr, xwdcolmap,
 
546
                                         error);
 
547
        }
 
548
      break;
 
549
 
 
550
    case 2:    /* Multiplane pixmaps */
 
551
      if ((depth == 1) && (bpp == 1))
 
552
        {
 
553
          image_ID = load_xwd_f2_d1_b1 (filename, ifp, &xwdhdr, xwdcolmap);
 
554
        }
 
555
      else if ((depth <= 8) && (bpp == 8))
 
556
        {
 
557
          image_ID = load_xwd_f2_d8_b8 (filename, ifp, &xwdhdr, xwdcolmap);
 
558
        }
 
559
      else if ((depth <= 16) && (bpp == 16))
 
560
        {
 
561
          image_ID = load_xwd_f2_d16_b16 (filename, ifp, &xwdhdr, xwdcolmap);
 
562
        }
 
563
      else if ((depth <= 24) && ((bpp == 24) || (bpp == 32)))
 
564
        {
 
565
          image_ID = load_xwd_f2_d24_b32 (filename, ifp, &xwdhdr, xwdcolmap,
 
566
                                          error);
 
567
        }
 
568
      break;
 
569
    }
 
570
  gimp_progress_update (1.0);
 
571
 
 
572
  fclose (ifp);
 
573
 
 
574
  if (xwdcolmap)
 
575
    g_free (xwdcolmap);
 
576
 
 
577
  if (image_ID == -1 && ! (error && *error))
 
578
    g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
 
579
                 _("XWD-file %s has format %d, depth %d and bits per pixel %d. "
 
580
                   "Currently this is not supported."),
 
581
                 gimp_filename_to_utf8 (filename),
 
582
                 (gint) xwdhdr.l_pixmap_format, depth, bpp);
 
583
 
 
584
  return image_ID;
 
585
}
 
586
 
 
587
static gint
 
588
save_image (const gchar  *filename,
 
589
            gint32        image_ID,
 
590
            gint32        drawable_ID,
 
591
            GError      **error)
 
592
{
 
593
  FILE          *ofp;
 
594
  GimpImageType  drawable_type;
 
595
  gint           retval;
 
596
 
 
597
  drawable_type = gimp_drawable_type (drawable_ID);
 
598
 
 
599
  /*  Make sure we're not saving an image with an alpha channel  */
 
600
  if (gimp_drawable_has_alpha (drawable_ID))
 
601
    {
 
602
      g_message (_("Cannot save images with alpha channels."));
 
603
      return FALSE;
 
604
    }
 
605
 
 
606
  switch (drawable_type)
 
607
    {
 
608
    case GIMP_INDEXED_IMAGE:
 
609
    case GIMP_GRAY_IMAGE:
 
610
    case GIMP_RGB_IMAGE:
 
611
      break;
 
612
    default:
 
613
      g_message (_("Cannot operate on unknown image types."));
 
614
      return FALSE;
 
615
      break;
 
616
    }
 
617
 
 
618
  /* Open the output file. */
 
619
  ofp = g_fopen (filename, "wb");
 
620
  if (! ofp)
 
621
    {
 
622
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
 
623
                   _("Could not open '%s' for writing: %s"),
 
624
                   gimp_filename_to_utf8 (filename), g_strerror (errno));
 
625
      return FALSE;
 
626
    }
 
627
 
 
628
  gimp_progress_init_printf (_("Saving '%s'"),
 
629
                             gimp_filename_to_utf8 (filename));
 
630
 
 
631
  switch (drawable_type)
 
632
    {
 
633
    case GIMP_INDEXED_IMAGE:
 
634
      retval = save_index (ofp, image_ID, drawable_ID, 0);
 
635
      break;
 
636
    case GIMP_GRAY_IMAGE:
 
637
      retval = save_index (ofp, image_ID, drawable_ID, 1);
 
638
      break;
 
639
    case GIMP_RGB_IMAGE:
 
640
      retval = save_rgb (ofp, image_ID, drawable_ID);
 
641
      break;
 
642
    default:
 
643
      retval = FALSE;
 
644
    }
 
645
 
 
646
  gimp_progress_update (1.0);
 
647
 
 
648
  fclose (ofp);
 
649
 
 
650
  return retval;
 
651
}
 
652
 
 
653
 
 
654
static L_CARD32
 
655
read_card32 (FILE *ifp,
 
656
             int  *err)
 
657
 
 
658
{
 
659
  L_CARD32 c;
 
660
 
 
661
  c =  (((L_CARD32) (getc (ifp))) << 24);
 
662
  c |= (((L_CARD32) (getc (ifp))) << 16);
 
663
  c |= (((L_CARD32) (getc (ifp))) << 8);
 
664
  c |= ((L_CARD32) (*err = getc (ifp)));
 
665
 
 
666
  *err = (*err < 0);
 
667
 
 
668
  return c;
 
669
}
 
670
 
 
671
 
 
672
static L_CARD16
 
673
read_card16 (FILE *ifp,
 
674
             int  *err)
 
675
 
 
676
{
 
677
  L_CARD16 c;
 
678
 
 
679
  c =  (((L_CARD16) (getc (ifp))) << 8);
 
680
  c |= ((L_CARD16) (*err = getc (ifp)));
 
681
 
 
682
  *err = (*err < 0);
 
683
 
 
684
  return c;
 
685
}
 
686
 
 
687
 
 
688
static L_CARD8
 
689
read_card8 (FILE *ifp,
 
690
            int  *err)
 
691
{
 
692
  L_CARD8 c;
 
693
 
 
694
  c = ((L_CARD8) (*err = getc (ifp)));
 
695
 
 
696
  *err = (*err < 0);
 
697
 
 
698
  return c;
 
699
}
 
700
 
 
701
static void
 
702
write_card32 (FILE     *ofp,
 
703
              L_CARD32  c)
 
704
{
 
705
  putc ((int) ((c >> 24) & 0xff), ofp);
 
706
  putc ((int) ((c >> 16) & 0xff), ofp);
 
707
  putc ((int) ((c >> 8) & 0xff), ofp);
 
708
  putc ((int) ((c) & 0xff), ofp);
 
709
}
 
710
 
 
711
 
 
712
static void
 
713
write_card16 (FILE     *ofp,
 
714
              L_CARD32  c)
 
715
{
 
716
  putc ((int) ((c >> 8) & 0xff), ofp);
 
717
  putc ((int) ((c) & 0xff), ofp);
 
718
}
 
719
 
 
720
 
 
721
static void
 
722
write_card8 (FILE     *ofp,
 
723
             L_CARD32  c)
 
724
{
 
725
  putc ((int) ((c) & 0xff), ofp);
 
726
}
 
727
 
 
728
 
 
729
static void
 
730
read_xwd_header (FILE            *ifp,
 
731
                 L_XWDFILEHEADER *xwdhdr)
 
732
{
 
733
  gint      j, err;
 
734
  L_CARD32 *cp;
 
735
 
 
736
  cp = (L_CARD32 *) xwdhdr;
 
737
 
 
738
  /* Read in all 32-bit values of the header and check for byte order */
 
739
  for (j = 0; j < sizeof (L_XWDFILEHEADER) / sizeof(xwdhdr->l_file_version); j++)
 
740
    {
 
741
      *(cp++) = read_card32 (ifp, &err);
 
742
 
 
743
      if (err)
 
744
        break;
 
745
    }
 
746
 
 
747
  if (err)
 
748
    xwdhdr->l_file_version = 0;  /* Not a valid XWD-file */
 
749
}
 
750
 
 
751
 
 
752
/* Write out an XWD-fileheader. The header size is calculated here */
 
753
static void
 
754
write_xwd_header (FILE            *ofp,
 
755
                  L_XWDFILEHEADER *xwdhdr)
 
756
 
 
757
{
 
758
  gint      j, hdrpad, hdr_entries;
 
759
  L_CARD32 *cp;
 
760
 
 
761
  hdrpad = XWDHDR_PAD;
 
762
  hdr_entries = sizeof (L_XWDFILEHEADER) / sizeof(xwdhdr->l_file_version);
 
763
  xwdhdr->l_header_size = hdr_entries * 4 + hdrpad;
 
764
 
 
765
  cp = (L_CARD32 *) xwdhdr;
 
766
 
 
767
  /* Write out all 32-bit values of the header and check for byte order */
 
768
  for (j = 0; j < sizeof (L_XWDFILEHEADER) / sizeof(xwdhdr->l_file_version); j++)
 
769
    {
 
770
      write_card32 (ofp, *(cp++));
 
771
    }
 
772
 
 
773
  /* Add padding bytes after XWD header */
 
774
  for (j = 0; j < hdrpad; j++)
 
775
    write_card8 (ofp, (L_CARD32) 0);
 
776
}
 
777
 
 
778
 
 
779
static void
 
780
read_xwd_cols (FILE            *ifp,
 
781
               L_XWDFILEHEADER *xwdhdr,
 
782
               L_XWDCOLOR      *colormap)
 
783
{
 
784
  gint  j, err = 0;
 
785
  gint  flag_is_bad, index_is_bad;
 
786
  gint  indexed = (xwdhdr->l_pixmap_depth <= 8);
 
787
  glong colmappos = ftell (ifp);
 
788
 
 
789
  /* Read in XWD-Color structures (the usual case) */
 
790
  flag_is_bad = index_is_bad = 0;
 
791
  for (j = 0; j < xwdhdr->l_ncolors; j++)
 
792
    {
 
793
      colormap[j].l_pixel = read_card32 (ifp, &err);
 
794
 
 
795
      colormap[j].l_red   = read_card16 (ifp, &err);
 
796
      colormap[j].l_green = read_card16 (ifp, &err);
 
797
      colormap[j].l_blue  = read_card16 (ifp, &err);
 
798
 
 
799
      colormap[j].l_flags = read_card8 (ifp, &err);
 
800
      colormap[j].l_pad   = read_card8 (ifp, &err);
 
801
 
 
802
      if (indexed && (colormap[j].l_pixel > 255))
 
803
        index_is_bad++;
 
804
 
 
805
      if (err)
 
806
        break;
 
807
    }
 
808
 
 
809
  if (err)        /* Not a valid XWD-file ? */
 
810
    xwdhdr->l_file_version = 0;
 
811
  if (err || ((flag_is_bad == 0) && (index_is_bad == 0)))
 
812
    return;
 
813
 
 
814
  /* Read in XWD-Color structures (with 4 bytes inserted infront of RGB) */
 
815
  fseek (ifp, colmappos, SEEK_SET);
 
816
  flag_is_bad = index_is_bad = 0;
 
817
  for (j = 0; j < xwdhdr->l_ncolors; j++)
 
818
    {
 
819
      colormap[j].l_pixel = read_card32 (ifp, &err);
 
820
 
 
821
      read_card32 (ifp, &err);  /* Empty bytes on Alpha OSF */
 
822
 
 
823
      colormap[j].l_red   = read_card16 (ifp, &err);
 
824
      colormap[j].l_green = read_card16 (ifp, &err);
 
825
      colormap[j].l_blue  = read_card16 (ifp, &err);
 
826
 
 
827
      colormap[j].l_flags = read_card8 (ifp, &err);
 
828
      colormap[j].l_pad   = read_card8 (ifp, &err);
 
829
 
 
830
      if (indexed && (colormap[j].l_pixel > 255))
 
831
        index_is_bad++;
 
832
 
 
833
      if (err)
 
834
        break;
 
835
    }
 
836
 
 
837
  if (err)        /* Not a valid XWD-file ? */
 
838
    xwdhdr->l_file_version = 0;
 
839
  if (err || ((flag_is_bad == 0) && (index_is_bad == 0)))
 
840
    return;
 
841
 
 
842
  /* Read in XWD-Color structures (with 2 bytes inserted infront of RGB) */
 
843
  fseek (ifp, colmappos, SEEK_SET);
 
844
  flag_is_bad = index_is_bad = 0;
 
845
  for (j = 0; j < xwdhdr->l_ncolors; j++)
 
846
    {
 
847
      colormap[j].l_pixel = read_card32 (ifp, &err);
 
848
 
 
849
      read_card16 (ifp, &err);  /* Empty bytes (from where ?) */
 
850
 
 
851
      colormap[j].l_red   = read_card16 (ifp, &err);
 
852
      colormap[j].l_green = read_card16 (ifp, &err);
 
853
      colormap[j].l_blue  = read_card16 (ifp, &err);
 
854
 
 
855
      colormap[j].l_flags = read_card8 (ifp, &err);
 
856
      colormap[j].l_pad   = read_card8 (ifp, &err);
 
857
 
 
858
      /* if ((colormap[j].l_flags == 0) || (colormap[j].l_flags > 7))
 
859
         flag_is_bad++; */
 
860
 
 
861
      if (indexed && (colormap[j].l_pixel > 255))
 
862
        index_is_bad++;
 
863
 
 
864
      if (err)
 
865
        break;
 
866
    }
 
867
 
 
868
  if (err)        /* Not a valid XWD-file ? */
 
869
    xwdhdr->l_file_version = 0;
 
870
  if (err || ((flag_is_bad == 0) && (index_is_bad == 0)))
 
871
    return;
 
872
 
 
873
  /* Read in XWD-Color structures (every value is 8 bytes from a CRAY) */
 
874
  fseek (ifp, colmappos, SEEK_SET);
 
875
  flag_is_bad = index_is_bad = 0;
 
876
  for (j = 0; j < xwdhdr->l_ncolors; j++)
 
877
    {
 
878
      read_card32 (ifp, &err);
 
879
      colormap[j].l_pixel = read_card32 (ifp, &err);
 
880
 
 
881
      read_card32 (ifp, &err);
 
882
      colormap[j].l_red   = read_card32 (ifp, &err);
 
883
      read_card32 (ifp, &err);
 
884
      colormap[j].l_green = read_card32 (ifp, &err);
 
885
      read_card32 (ifp, &err);
 
886
      colormap[j].l_blue  = read_card32 (ifp, &err);
 
887
 
 
888
      /* The flag byte is kept in the first byte */
 
889
      colormap[j].l_flags = read_card8 (ifp, &err);
 
890
      colormap[j].l_pad   = read_card8 (ifp, &err);
 
891
      read_card16 (ifp, &err);
 
892
      read_card32 (ifp, &err);
 
893
 
 
894
      if (indexed && (colormap[j].l_pixel > 255))
 
895
        index_is_bad++;
 
896
 
 
897
       if (err)
 
898
         break;
 
899
    }
 
900
 
 
901
  if (flag_is_bad || index_is_bad)
 
902
    {
 
903
      printf ("xwd: Warning. Error in XWD-color-structure (");
 
904
 
 
905
      if (flag_is_bad) printf ("flag");
 
906
      if (index_is_bad) printf ("index");
 
907
 
 
908
      printf (")\n");
 
909
    }
 
910
 
 
911
  if (err)
 
912
    xwdhdr->l_file_version = 0;  /* Not a valid XWD-file */
 
913
}
 
914
 
 
915
 
 
916
static void
 
917
write_xwd_cols (FILE            *ofp,
 
918
                L_XWDFILEHEADER *xwdhdr,
 
919
                L_XWDCOLOR      *colormap)
 
920
 
 
921
{
 
922
  int j;
 
923
 
 
924
  for (j = 0; j < xwdhdr->l_colormap_entries; j++)
 
925
    {
 
926
#ifdef CRAY
 
927
      write_card32 (ofp, (L_CARD32)0);
 
928
      write_card32 (ofp, colormap[j].l_pixel);
 
929
      write_card32 (ofp, (L_CARD32)0);
 
930
      write_card32 (ofp, (L_CARD32)colormap[j].l_red);
 
931
      write_card32 (ofp, (L_CARD32)0);
 
932
      write_card32 (ofp, (L_CARD32)colormap[j].l_green);
 
933
      write_card32 (ofp, (L_CARD32)0);
 
934
      write_card32 (ofp, (L_CARD32)colormap[j].l_blue);
 
935
      write_card8  (ofp, (L_CARD32)colormap[j].l_flags);
 
936
      write_card8  (ofp, (L_CARD32)colormap[j].l_pad);
 
937
      write_card16 (ofp, (L_CARD32)0);
 
938
      write_card32 (ofp, (L_CARD32)0);
 
939
#else
 
940
#ifdef __alpha
 
941
      write_card32 (ofp, colormap[j].l_pixel);
 
942
      write_card32 (ofp, (L_CARD32)0);
 
943
      write_card16 (ofp, (L_CARD32)colormap[j].l_red);
 
944
      write_card16 (ofp, (L_CARD32)colormap[j].l_green);
 
945
      write_card16 (ofp, (L_CARD32)colormap[j].l_blue);
 
946
      write_card8  (ofp, (L_CARD32)colormap[j].l_flags);
 
947
      write_card8  (ofp, (L_CARD32)colormap[j].l_pad);
 
948
#else
 
949
      write_card32 (ofp, colormap[j].l_pixel);
 
950
      write_card16 (ofp, (L_CARD32)colormap[j].l_red);
 
951
      write_card16 (ofp, (L_CARD32)colormap[j].l_green);
 
952
      write_card16 (ofp, (L_CARD32)colormap[j].l_blue);
 
953
      write_card8  (ofp, (L_CARD32)colormap[j].l_flags);
 
954
      write_card8  (ofp, (L_CARD32)colormap[j].l_pad);
 
955
#endif
 
956
#endif
 
957
    }
 
958
}
 
959
 
 
960
 
 
961
/* Create a map for mapping up to 32 bit pixelvalues to RGB. */
 
962
/* Returns number of colors kept in the map (up to 256) */
 
963
 
 
964
static gint
 
965
set_pixelmap (int         ncols,
 
966
              L_XWDCOLOR *xwdcol,
 
967
              PIXEL_MAP  *pixelmap)
 
968
 
 
969
{
 
970
  gint     i, j, k, maxcols;
 
971
  L_CARD32 pixel_val;
 
972
 
 
973
  memset ((gchar *) pixelmap, 0, sizeof (PIXEL_MAP));
 
974
 
 
975
  maxcols = 0;
 
976
 
 
977
  for (j = 0; j < ncols; j++) /* For each entry of the XWD colormap */
 
978
    {
 
979
      pixel_val = xwdcol[j].l_pixel;
 
980
      for (k = 0; k < maxcols; k++)  /* Where to insert in list ? */
 
981
        {
 
982
          if (pixel_val <= pixelmap->pmap[k].pixel_val)
 
983
            break;
 
984
        }
 
985
      if ((k < maxcols) && (pixel_val == pixelmap->pmap[k].pixel_val))
 
986
        break;   /* It was already in list */
 
987
 
 
988
      if (k >= 256)
 
989
        break;
 
990
 
 
991
      if (k < maxcols)   /* Must move entries to the back ? */
 
992
        {
 
993
          for (i = maxcols-1; i >= k; i--)
 
994
            memcpy ((char *)&(pixelmap->pmap[i+1]),(char *)&(pixelmap->pmap[i]),
 
995
                    sizeof (PMAP));
 
996
        }
 
997
      pixelmap->pmap[k].pixel_val = pixel_val;
 
998
      pixelmap->pmap[k].red = xwdcol[j].l_red >> 8;
 
999
      pixelmap->pmap[k].green = xwdcol[j].l_green >> 8;
 
1000
      pixelmap->pmap[k].blue = xwdcol[j].l_blue >> 8;
 
1001
      pixelmap->pixel_in_map[pixel_val & MAPPERMASK] = 1;
 
1002
      maxcols++;
 
1003
    }
 
1004
  pixelmap->npixel = maxcols;
 
1005
#ifdef XWD_COL_DEBUG
 
1006
  printf ("Colors in pixelmap: %d\n",pixelmap->npixel);
 
1007
  for (j=0; j<pixelmap->npixel; j++)
 
1008
    printf ("Pixelvalue 0x%08lx, 0x%02x 0x%02x 0x%02x\n",
 
1009
            pixelmap->pmap[j].pixel_val,
 
1010
            pixelmap->pmap[j].red,pixelmap->pmap[j].green,
 
1011
            pixelmap->pmap[j].blue);
 
1012
  for (j=0; j<=MAPPERMASK; j++)
 
1013
    printf ("0x%08lx: %d\n",(long)j,pixelmap->pixel_in_map[j]);
 
1014
#endif
 
1015
 
 
1016
  return pixelmap->npixel;
 
1017
}
 
1018
 
 
1019
 
 
1020
/* Search a pixel value in the pixel map. Returns FALSE if the */
 
1021
/* pixelval was not found in map. Returns TRUE if found. */
 
1022
 
 
1023
static gboolean
 
1024
get_pixelmap (L_CARD32   pixelval,
 
1025
              PIXEL_MAP *pixelmap,
 
1026
              guchar    *red,
 
1027
              guchar    *green,
 
1028
              guchar    *blue)
 
1029
 
 
1030
{
 
1031
  register PMAP *low, *high, *middle;
 
1032
 
 
1033
  if (pixelmap->npixel == 0)
 
1034
    return FALSE;
 
1035
 
 
1036
  if (!(pixelmap->pixel_in_map[pixelval & MAPPERMASK]))
 
1037
    return FALSE;
 
1038
 
 
1039
  low =  &(pixelmap->pmap[0]);
 
1040
  high = &(pixelmap->pmap[pixelmap->npixel-1]);
 
1041
 
 
1042
  /* Do a binary search on the array */
 
1043
  while (low < high)
 
1044
    {
 
1045
      middle = low + ((high - low)/2);
 
1046
      if (pixelval <= middle->pixel_val)
 
1047
        high = middle;
 
1048
      else
 
1049
        low = middle+1;
 
1050
    }
 
1051
 
 
1052
  if (pixelval == low->pixel_val)
 
1053
    {
 
1054
      *red = low->red; *green = low->green; *blue = low->blue;
 
1055
      return TRUE;
 
1056
    }
 
1057
 
 
1058
  return FALSE;
 
1059
}
 
1060
 
 
1061
 
 
1062
static void
 
1063
set_bw_color_table (gint32 image_ID)
 
1064
{
 
1065
  static guchar BWColorMap[2*3] = { 255, 255, 255, 0, 0, 0 };
 
1066
 
 
1067
#ifdef XWD_COL_DEBUG
 
1068
  printf ("Set GIMP b/w-colortable:\n");
 
1069
#endif
 
1070
 
 
1071
  gimp_image_set_colormap (image_ID, BWColorMap, 2);
 
1072
}
 
1073
 
 
1074
 
 
1075
/* Initialize an 8-bit colortable from the mask-values of the XWD-header */
 
1076
static void
 
1077
init_color_table256 (L_XWDFILEHEADER *xwdhdr,
 
1078
                     guchar          *ColorMap)
 
1079
 
 
1080
{
 
1081
  gint i, j, k, cuind;
 
1082
  gint redshift, greenshift, blueshift;
 
1083
  gint maxred, maxgreen, maxblue;
 
1084
 
 
1085
  /* Assume: the bit masks for red/green/blue are grouped together
 
1086
   * Example: redmask = 0xe0, greenmask = 0x1c, bluemask = 0x03
 
1087
   * We need to know where to place the RGB-values (shifting)
 
1088
   * and the maximum value for each component.
 
1089
   */
 
1090
  redshift = greenshift = blueshift = 0;
 
1091
  if ((maxred = xwdhdr->l_red_mask) == 0)
 
1092
    return;
 
1093
 
 
1094
  /* Shift the redmask to the rightmost bit position to get
 
1095
   * maximum value for red.
 
1096
   */
 
1097
  while ((maxred & 1) == 0)
 
1098
    {
 
1099
      redshift++;
 
1100
      maxred >>= 1;
 
1101
    }
 
1102
 
 
1103
  if ((maxgreen = xwdhdr->l_green_mask) == 0)
 
1104
    return;
 
1105
 
 
1106
  while ((maxgreen & 1) == 0)
 
1107
    {
 
1108
      greenshift++;
 
1109
      maxgreen >>= 1;
 
1110
    }
 
1111
 
 
1112
  if ((maxblue = xwdhdr->l_blue_mask) == 0)
 
1113
    return;
 
1114
 
 
1115
  while ((maxblue & 1) == 0)
 
1116
    {
 
1117
      blueshift++;
 
1118
      maxblue >>= 1;
 
1119
    }
 
1120
 
 
1121
  memset ((gchar *) ColorMap, 0, 256 * 3);
 
1122
 
 
1123
  for (i = 0; i <= maxred; i++)
 
1124
    for (j = 0; j <= maxgreen; j++)
 
1125
      for (k = 0; k <= maxblue; k++)
 
1126
        {
 
1127
          cuind = (i << redshift) | (j << greenshift) | (k << blueshift);
 
1128
 
 
1129
          if (cuind < 256)
 
1130
            {
 
1131
              ColorMap[cuind*3]   = (i * 255)/maxred;
 
1132
              ColorMap[cuind*3+1] = (j * 255)/maxgreen;
 
1133
              ColorMap[cuind*3+2] = (k * 255)/maxblue;
 
1134
            }
 
1135
        }
 
1136
}
 
1137
 
 
1138
 
 
1139
static void
 
1140
set_color_table (gint32           image_ID,
 
1141
                 L_XWDFILEHEADER *xwdhdr,
 
1142
                 L_XWDCOLOR      *xwdcolmap)
 
1143
 
 
1144
{
 
1145
  gint   ncols, i, j;
 
1146
  guchar ColorMap[256 * 3];
 
1147
 
 
1148
  ncols = xwdhdr->l_colormap_entries;
 
1149
  if (xwdhdr->l_ncolors < ncols)
 
1150
    ncols = xwdhdr->l_ncolors;
 
1151
  if (ncols <= 0)
 
1152
    return;
 
1153
  if (ncols > 256)
 
1154
    ncols = 256;
 
1155
 
 
1156
  /* Initialize color table for all 256 entries from mask-values */
 
1157
  init_color_table256 (xwdhdr, ColorMap);
 
1158
 
 
1159
  for (j = 0; j < ncols; j++)
 
1160
    {
 
1161
      i = xwdcolmap[j].l_pixel;
 
1162
      if ((i >= 0) && (i < 256))
 
1163
        {
 
1164
          ColorMap[i*3] = (xwdcolmap[j].l_red) >> 8;
 
1165
          ColorMap[i*3+1] = (xwdcolmap[j].l_green) >> 8;
 
1166
          ColorMap[i*3+2] = (xwdcolmap[j].l_blue) >> 8;
 
1167
        }
 
1168
    }
 
1169
 
 
1170
#ifdef XWD_COL_DEBUG
 
1171
  printf ("Set GIMP colortable:\n");
 
1172
  for (j = 0; j < 256; j++)
 
1173
    printf ("%3d: 0x%02x 0x%02x 0x%02x\n", j,
 
1174
            ColorMap[j*3], ColorMap[j*3+1], ColorMap[j*3+2]);
 
1175
#endif
 
1176
 
 
1177
  gimp_image_set_colormap (image_ID, ColorMap, 256);
 
1178
}
 
1179
 
 
1180
 
 
1181
 
 
1182
 
 
1183
/* Create an image. Sets layer_ID, drawable and rgn. Returns image_ID */
 
1184
static gint32
 
1185
create_new_image (const gchar         *filename,
 
1186
                  guint                width,
 
1187
                  guint                height,
 
1188
                  GimpImageBaseType    type,
 
1189
                  gint32              *layer_ID,
 
1190
                  GimpDrawable       **drawable,
 
1191
                  GimpPixelRgn        *pixel_rgn)
 
1192
{
 
1193
  gint32        image_ID;
 
1194
  GimpImageType gdtype;
 
1195
 
 
1196
  switch (type)
 
1197
    {
 
1198
    case GIMP_GRAY:
 
1199
      gdtype = GIMP_GRAY_IMAGE;
 
1200
      break;
 
1201
    case GIMP_INDEXED:
 
1202
      gdtype = GIMP_INDEXED_IMAGE;
 
1203
      break;
 
1204
    case GIMP_RGB:
 
1205
      gdtype = GIMP_RGB_IMAGE;
 
1206
      break;
 
1207
    default:
 
1208
      g_warning ("Unknown image type");
 
1209
      return -1;
 
1210
    }
 
1211
 
 
1212
  image_ID = gimp_image_new (width, height, type);
 
1213
  gimp_image_set_filename (image_ID, filename);
 
1214
 
 
1215
  *layer_ID = gimp_layer_new (image_ID, "Background", width, height,
 
1216
                              gdtype, 100, GIMP_NORMAL_MODE);
 
1217
  gimp_image_insert_layer (image_ID, *layer_ID, -1, 0);
 
1218
 
 
1219
  *drawable = gimp_drawable_get (*layer_ID);
 
1220
  gimp_pixel_rgn_init (pixel_rgn, *drawable, 0, 0, (*drawable)->width,
 
1221
                       (*drawable)->height, TRUE, FALSE);
 
1222
 
 
1223
  return image_ID;
 
1224
}
 
1225
 
 
1226
 
 
1227
/* Load XWD with pixmap_format 2, pixmap_depth 1, bits_per_pixel 1 */
 
1228
 
 
1229
static gint32
 
1230
load_xwd_f2_d1_b1 (const gchar     *filename,
 
1231
                   FILE            *ifp,
 
1232
                   L_XWDFILEHEADER *xwdhdr,
 
1233
                   L_XWDCOLOR      *xwdcolmap)
 
1234
{
 
1235
  register int     pix8;
 
1236
  register guchar *dest, *src;
 
1237
  guchar           c1, c2, c3, c4;
 
1238
  gint             width, height, scan_lines, tile_height;
 
1239
  gint             i, j, ncols;
 
1240
  gchar           *temp;
 
1241
  guchar           bit2byte[256 * 8];
 
1242
  guchar          *data, *scanline;
 
1243
  gint             err = 0;
 
1244
  gint32           layer_ID, image_ID;
 
1245
  GimpPixelRgn     pixel_rgn;
 
1246
  GimpDrawable    *drawable;
 
1247
 
 
1248
#ifdef XWD_DEBUG
 
1249
  printf ("load_xwd_f2_d1_b1 (%s)\n", filename);
 
1250
#endif
 
1251
 
 
1252
  width  = xwdhdr->l_pixmap_width;
 
1253
  height = xwdhdr->l_pixmap_height;
 
1254
 
 
1255
  image_ID = create_new_image (filename, width, height, GIMP_INDEXED,
 
1256
                               &layer_ID, &drawable, &pixel_rgn);
 
1257
 
 
1258
  tile_height = gimp_tile_height ();
 
1259
  data = g_malloc (tile_height * width);
 
1260
 
 
1261
  scanline = g_new (guchar, xwdhdr->l_bytes_per_line + 8);
 
1262
 
 
1263
  ncols = xwdhdr->l_colormap_entries;
 
1264
  if (xwdhdr->l_ncolors < ncols)
 
1265
    ncols = xwdhdr->l_ncolors;
 
1266
 
 
1267
  if (ncols < 2)
 
1268
    set_bw_color_table (image_ID);
 
1269
  else
 
1270
    set_color_table (image_ID, xwdhdr, xwdcolmap);
 
1271
 
 
1272
  temp = (gchar *) bit2byte;
 
1273
 
 
1274
  /* Get an array for mapping 8 bits in a byte to 8 bytes */
 
1275
  if (!xwdhdr->l_bitmap_bit_order)
 
1276
    {
 
1277
      for (j = 0; j < 256; j++)
 
1278
        for (i = 0; i < 8; i++)
 
1279
          *(temp++) = ((j & (1 << i)) != 0);
 
1280
    }
 
1281
  else
 
1282
    {
 
1283
      for (j = 0; j < 256; j++)
 
1284
        for (i = 7; i >= 0; i--)
 
1285
          *(temp++) = ((j & (1 << i)) != 0);
 
1286
    }
 
1287
 
 
1288
  dest = data;
 
1289
  scan_lines = 0;
 
1290
 
 
1291
  for (i = 0; i < height; i++)
 
1292
    {
 
1293
      if (fread (scanline, xwdhdr->l_bytes_per_line, 1, ifp) != 1)
 
1294
        {
 
1295
          err = 1;
 
1296
          break;
 
1297
        }
 
1298
 
 
1299
      /* Need to check byte order ? */
 
1300
      if (xwdhdr->l_bitmap_bit_order != xwdhdr->l_byte_order)
 
1301
        {
 
1302
          src = scanline;
 
1303
          switch (xwdhdr->l_bitmap_unit)
 
1304
            {
 
1305
            case 16:
 
1306
              j = xwdhdr->l_bytes_per_line;
 
1307
              while (j > 0)
 
1308
                {
 
1309
                  c1 = src[0]; c2 = src[1];
 
1310
                  *(src++) = c2; *(src++) = c1;
 
1311
                  j -= 2;
 
1312
                }
 
1313
              break;
 
1314
 
 
1315
            case 32:
 
1316
              j = xwdhdr->l_bytes_per_line;
 
1317
              while (j > 0)
 
1318
                {
 
1319
                  c1 = src[0]; c2 = src[1]; c3 = src[2]; c4 = src[3];
 
1320
                  *(src++) = c4; *(src++) = c3; *(src++) = c2; *(src++) = c1;
 
1321
                  j -= 4;
 
1322
                }
 
1323
              break;
 
1324
            }
 
1325
        }
 
1326
      src = scanline;
 
1327
      j = width;
 
1328
      while (j >= 8)
 
1329
        {
 
1330
          pix8 = *(src++);
 
1331
          memcpy (dest, bit2byte + pix8*8, 8);
 
1332
          dest += 8;
 
1333
          j -= 8;
 
1334
        }
 
1335
      if (j > 0)
 
1336
        {
 
1337
          pix8 = *(src++);
 
1338
          memcpy (dest, bit2byte + pix8*8, j);
 
1339
          dest += j;
 
1340
        }
 
1341
 
 
1342
      scan_lines++;
 
1343
 
 
1344
      if ((i % 20) == 0)
 
1345
        gimp_progress_update ((double)(i+1) / (double)height);
 
1346
 
 
1347
      if ((scan_lines == tile_height) || ((i+1) == height))
 
1348
        {
 
1349
          gimp_pixel_rgn_set_rect (&pixel_rgn, data, 0, i-scan_lines+1,
 
1350
                                   width, scan_lines);
 
1351
          scan_lines = 0;
 
1352
          dest = data;
 
1353
        }
 
1354
      if (err) break;
 
1355
    }
 
1356
 
 
1357
  g_free (data);
 
1358
  g_free (scanline);
 
1359
 
 
1360
  if (err)
 
1361
    g_message (_("EOF encountered on reading"));
 
1362
 
 
1363
  gimp_drawable_flush (drawable);
 
1364
 
 
1365
  return err ? -1 : image_ID;
 
1366
}
 
1367
 
 
1368
 
 
1369
/* Load XWD with pixmap_format 2, pixmap_depth 8, bits_per_pixel 8 */
 
1370
 
 
1371
static gint32
 
1372
load_xwd_f2_d8_b8 (const gchar     *filename,
 
1373
                   FILE            *ifp,
 
1374
                   L_XWDFILEHEADER *xwdhdr,
 
1375
                   L_XWDCOLOR      *xwdcolmap)
 
1376
{
 
1377
  gint          width, height, linepad, tile_height, scan_lines;
 
1378
  gint          i, j, ncols;
 
1379
  gint          grayscale;
 
1380
  guchar       *dest, *data;
 
1381
  gint          err = 0;
 
1382
  gint32        layer_ID, image_ID;
 
1383
  GimpPixelRgn  pixel_rgn;
 
1384
  GimpDrawable *drawable;
 
1385
 
 
1386
#ifdef XWD_DEBUG
 
1387
  printf ("load_xwd_f2_d8_b8 (%s)\n", filename);
 
1388
#endif
 
1389
 
 
1390
  width  = xwdhdr->l_pixmap_width;
 
1391
  height = xwdhdr->l_pixmap_height;
 
1392
 
 
1393
  /* This could also be a grayscale image. Check it */
 
1394
  grayscale = 0;
 
1395
  if ((xwdhdr->l_ncolors == 256) && (xwdhdr->l_colormap_entries == 256))
 
1396
    {
 
1397
      for (j = 0; j < 256; j++)
 
1398
        {
 
1399
          if ((xwdcolmap[j].l_pixel != j)
 
1400
              || ((xwdcolmap[j].l_red >> 8) != j)
 
1401
              || ((xwdcolmap[j].l_green >> 8) != j)
 
1402
              || ((xwdcolmap[j].l_blue >> 8) != j))
 
1403
            break;
 
1404
        }
 
1405
 
 
1406
      grayscale = (j == 256);
 
1407
    }
 
1408
 
 
1409
  image_ID = create_new_image (filename, width, height,
 
1410
                               grayscale ? GIMP_GRAY : GIMP_INDEXED,
 
1411
                               &layer_ID, &drawable, &pixel_rgn);
 
1412
 
 
1413
  tile_height = gimp_tile_height ();
 
1414
  data = g_malloc (tile_height * width);
 
1415
 
 
1416
  if (!grayscale)
 
1417
    {
 
1418
      ncols = xwdhdr->l_colormap_entries;
 
1419
      if (xwdhdr->l_ncolors < ncols) ncols = xwdhdr->l_ncolors;
 
1420
      if (ncols < 2)
 
1421
        set_bw_color_table (image_ID);
 
1422
      else
 
1423
        set_color_table (image_ID, xwdhdr, xwdcolmap);
 
1424
    }
 
1425
 
 
1426
  linepad = xwdhdr->l_bytes_per_line - xwdhdr->l_pixmap_width;
 
1427
  if (linepad < 0)
 
1428
    linepad = 0;
 
1429
 
 
1430
  dest = data;
 
1431
  scan_lines = 0;
 
1432
 
 
1433
  for (i = 0; i < height; i++)
 
1434
    {
 
1435
      if (fread (dest, 1, width, ifp) != width)
 
1436
        {
 
1437
          err = 1;
 
1438
          break;
 
1439
        }
 
1440
      dest += width;
 
1441
 
 
1442
      for (j = 0; j < linepad; j++)
 
1443
        getc (ifp);
 
1444
 
 
1445
      scan_lines++;
 
1446
 
 
1447
      if ((i % 20) == 0)
 
1448
        gimp_progress_update ((gdouble) (i + 1) / (gdouble) height);
 
1449
 
 
1450
      if ((scan_lines == tile_height) || ((i+1) == height))
 
1451
        {
 
1452
          gimp_pixel_rgn_set_rect (&pixel_rgn, data, 0, i-scan_lines+1,
 
1453
                                   width, scan_lines);
 
1454
          scan_lines = 0;
 
1455
          dest = data;
 
1456
        }
 
1457
    }
 
1458
 
 
1459
  g_free (data);
 
1460
 
 
1461
  if (err)
 
1462
    g_message (_("EOF encountered on reading"));
 
1463
 
 
1464
  gimp_drawable_flush (drawable);
 
1465
 
 
1466
  return err ? -1 : image_ID;
 
1467
}
 
1468
 
 
1469
 
 
1470
/* Load XWD with pixmap_format 2, pixmap_depth up to 16, bits_per_pixel 16 */
 
1471
 
 
1472
static gint32
 
1473
load_xwd_f2_d16_b16 (const gchar     *filename,
 
1474
                     FILE            *ifp,
 
1475
                     L_XWDFILEHEADER *xwdhdr,
 
1476
                     L_XWDCOLOR      *xwdcolmap)
 
1477
{
 
1478
  register guchar *dest, lsbyte_first;
 
1479
  gint             width, height, linepad, i, j, c0, c1, ncols;
 
1480
  gint             red, green, blue, redval, greenval, blueval;
 
1481
  gint             maxred, maxgreen, maxblue;
 
1482
  gint             tile_height, scan_lines;
 
1483
  gulong           redmask, greenmask, bluemask;
 
1484
  guint            redshift, greenshift, blueshift;
 
1485
  gulong           maxval;
 
1486
  guchar          *ColorMap, *cm, *data;
 
1487
  gint             err = 0;
 
1488
  gint32           layer_ID, image_ID;
 
1489
  GimpPixelRgn     pixel_rgn;
 
1490
  GimpDrawable    *drawable;
 
1491
 
 
1492
#ifdef XWD_DEBUG
 
1493
  printf ("load_xwd_f2_d16_b16 (%s)\n", filename);
 
1494
#endif
 
1495
 
 
1496
  width  = xwdhdr->l_pixmap_width;
 
1497
  height = xwdhdr->l_pixmap_height;
 
1498
 
 
1499
  image_ID = create_new_image (filename, width, height, GIMP_RGB,
 
1500
                               &layer_ID, &drawable, &pixel_rgn);
 
1501
 
 
1502
  tile_height = gimp_tile_height ();
 
1503
  data = g_malloc (tile_height * width * 3);
 
1504
 
 
1505
  /* Get memory for mapping 16 bit XWD-pixel to GIMP-RGB */
 
1506
  maxval = 0x10000 * 3;
 
1507
  ColorMap = g_new0 (guchar, maxval);
 
1508
 
 
1509
  redmask   = xwdhdr->l_red_mask;
 
1510
  greenmask = xwdhdr->l_green_mask;
 
1511
  bluemask  = xwdhdr->l_blue_mask;
 
1512
 
 
1513
  /* How to shift RGB to be right aligned ? */
 
1514
  /* (We rely on the the mask bits are grouped and not mixed) */
 
1515
  redshift = greenshift = blueshift = 0;
 
1516
 
 
1517
  while (((1 << redshift)   & redmask)   == 0) redshift++;
 
1518
  while (((1 << greenshift) & greenmask) == 0) greenshift++;
 
1519
  while (((1 << blueshift)  & bluemask)  == 0) blueshift++;
 
1520
 
 
1521
  /* The bits_per_rgb may not be correct. Use redmask instead */
 
1522
  maxred = 0; while (redmask >> (redshift + maxred)) maxred++;
 
1523
  maxred = (1 << maxred) - 1;
 
1524
 
 
1525
  maxgreen = 0; while (greenmask >> (greenshift + maxgreen)) maxgreen++;
 
1526
  maxgreen = (1 << maxgreen) - 1;
 
1527
 
 
1528
  maxblue = 0; while (bluemask >> (blueshift + maxblue)) maxblue++;
 
1529
  maxblue = (1 << maxblue) - 1;
 
1530
 
 
1531
  /* Built up the array to map XWD-pixel value to GIMP-RGB */
 
1532
  for (red = 0; red <= maxred; red++)
 
1533
    {
 
1534
      redval = (red * 255) / maxred;
 
1535
      for (green = 0; green <= maxgreen; green++)
 
1536
        {
 
1537
          greenval = (green * 255) / maxgreen;
 
1538
          for (blue = 0; blue <= maxblue; blue++)
 
1539
            {
 
1540
              blueval = (blue * 255) / maxblue;
 
1541
              cm = ColorMap + ((red << redshift) + (green << greenshift)
 
1542
                               + (blue << blueshift)) * 3;
 
1543
              *(cm++) = redval;
 
1544
              *(cm++) = greenval;
 
1545
              *cm = blueval;
 
1546
            }
 
1547
        }
 
1548
    }
 
1549
 
 
1550
  /* Now look what was written to the XWD-Colormap */
 
1551
 
 
1552
  ncols = xwdhdr->l_colormap_entries;
 
1553
  if (xwdhdr->l_ncolors < ncols)
 
1554
    ncols = xwdhdr->l_ncolors;
 
1555
 
 
1556
  for (j = 0; j < ncols; j++)
 
1557
    {
 
1558
      cm = ColorMap + xwdcolmap[j].l_pixel * 3;
 
1559
      *(cm++) = (xwdcolmap[j].l_red >> 8);
 
1560
      *(cm++) = (xwdcolmap[j].l_green >> 8);
 
1561
      *cm = (xwdcolmap[j].l_blue >> 8);
 
1562
    }
 
1563
 
 
1564
  /* What do we have to consume after a line has finished ? */
 
1565
  linepad =   xwdhdr->l_bytes_per_line
 
1566
    - (xwdhdr->l_pixmap_width*xwdhdr->l_bits_per_pixel)/8;
 
1567
  if (linepad < 0) linepad = 0;
 
1568
 
 
1569
  lsbyte_first = (xwdhdr->l_byte_order == 0);
 
1570
 
 
1571
  dest = data;
 
1572
  scan_lines = 0;
 
1573
 
 
1574
  for (i = 0; i < height; i++)
 
1575
    {
 
1576
      for (j = 0; j < width; j++)
 
1577
        {
 
1578
          c0 = getc (ifp);
 
1579
          c1 = getc (ifp);
 
1580
          if (c1 < 0)
 
1581
            {
 
1582
              err = 1;
 
1583
              break;
 
1584
            }
 
1585
 
 
1586
          if (lsbyte_first)
 
1587
            c0 = c0 | (c1 << 8);
 
1588
          else
 
1589
            c0 = (c0 << 8) | c1;
 
1590
 
 
1591
          cm = ColorMap + c0 * 3;
 
1592
          *(dest++) = *(cm++);
 
1593
          *(dest++) = *(cm++);
 
1594
          *(dest++) = *cm;
 
1595
        }
 
1596
 
 
1597
      if (err)
 
1598
        break;
 
1599
 
 
1600
      for (j = 0; j < linepad; j++)
 
1601
        getc (ifp);
 
1602
 
 
1603
      scan_lines++;
 
1604
 
 
1605
      if ((i % 20) == 0)
 
1606
        gimp_progress_update ((double)(i+1) / (double)height);
 
1607
 
 
1608
      if ((scan_lines == tile_height) || ((i+1) == height))
 
1609
        {
 
1610
          gimp_pixel_rgn_set_rect (&pixel_rgn, data, 0, i-scan_lines+1,
 
1611
                                   width, scan_lines);
 
1612
          scan_lines = 0;
 
1613
          dest = data;
 
1614
        }
 
1615
    }
 
1616
  g_free (data);
 
1617
  g_free (ColorMap);
 
1618
 
 
1619
  if (err)
 
1620
    g_message (_("EOF encountered on reading"));
 
1621
 
 
1622
  gimp_drawable_flush (drawable);
 
1623
 
 
1624
  return err ? -1 : image_ID;
 
1625
}
 
1626
 
 
1627
 
 
1628
/* Load XWD with pixmap_format 2, pixmap_depth up to 24, bits_per_pixel 24/32 */
 
1629
 
 
1630
static gint32
 
1631
load_xwd_f2_d24_b32 (const gchar      *filename,
 
1632
                     FILE             *ifp,
 
1633
                     L_XWDFILEHEADER  *xwdhdr,
 
1634
                     L_XWDCOLOR       *xwdcolmap,
 
1635
                     GError          **error)
 
1636
{
 
1637
  register guchar *dest, lsbyte_first;
 
1638
  gint             width, height, linepad, i, j, c0, c1, c2, c3;
 
1639
  gint             tile_height, scan_lines;
 
1640
  L_CARD32         pixelval;
 
1641
  gint             red, green, blue, ncols;
 
1642
  gint             maxred, maxgreen, maxblue;
 
1643
  gulong           redmask, greenmask, bluemask;
 
1644
  guint            redshift, greenshift, blueshift;
 
1645
  guchar           redmap[256], greenmap[256], bluemap[256];
 
1646
  guchar          *data;
 
1647
  PIXEL_MAP        pixel_map;
 
1648
  gint             err = 0;
 
1649
  gint32           layer_ID, image_ID;
 
1650
  GimpPixelRgn     pixel_rgn;
 
1651
  GimpDrawable    *drawable;
 
1652
 
 
1653
#ifdef XWD_DEBUG
 
1654
  printf ("load_xwd_f2_d24_b32 (%s)\n", filename);
 
1655
#endif
 
1656
 
 
1657
  width  = xwdhdr->l_pixmap_width;
 
1658
  height = xwdhdr->l_pixmap_height;
 
1659
 
 
1660
  redmask   = xwdhdr->l_red_mask;
 
1661
  greenmask = xwdhdr->l_green_mask;
 
1662
  bluemask  = xwdhdr->l_blue_mask;
 
1663
 
 
1664
  if (redmask   == 0) redmask   = 0xff0000;
 
1665
  if (greenmask == 0) greenmask = 0x00ff00;
 
1666
  if (bluemask  == 0) bluemask  = 0x0000ff;
 
1667
 
 
1668
  /* How to shift RGB to be right aligned ? */
 
1669
  /* (We rely on the the mask bits are grouped and not mixed) */
 
1670
  redshift = greenshift = blueshift = 0;
 
1671
 
 
1672
  while (((1 << redshift)   & redmask)   == 0) redshift++;
 
1673
  while (((1 << greenshift) & greenmask) == 0) greenshift++;
 
1674
  while (((1 << blueshift)  & bluemask)  == 0) blueshift++;
 
1675
 
 
1676
  /* The bits_per_rgb may not be correct. Use redmask instead */
 
1677
 
 
1678
  maxred = 0; while (redmask >> (redshift + maxred)) maxred++;
 
1679
  maxred = (1 << maxred) - 1;
 
1680
 
 
1681
  maxgreen = 0; while (greenmask >> (greenshift + maxgreen)) maxgreen++;
 
1682
  maxgreen = (1 << maxgreen) - 1;
 
1683
 
 
1684
  maxblue = 0; while (bluemask >> (blueshift + maxblue)) maxblue++;
 
1685
  maxblue = (1 << maxblue) - 1;
 
1686
 
 
1687
  if (maxred   > sizeof (redmap)   ||
 
1688
      maxgreen > sizeof (greenmap) ||
 
1689
      maxblue  > sizeof (bluemap))
 
1690
    {
 
1691
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
 
1692
                   _("XWD-file %s is corrupt."),
 
1693
                   gimp_filename_to_utf8 (filename));
 
1694
      return -1;
 
1695
    }
 
1696
 
 
1697
  image_ID = create_new_image (filename, width, height, GIMP_RGB,
 
1698
                               &layer_ID, &drawable, &pixel_rgn);
 
1699
 
 
1700
  tile_height = gimp_tile_height ();
 
1701
  data = g_malloc (tile_height * width * 3);
 
1702
 
 
1703
  /* Set map-arrays for red, green, blue */
 
1704
  for (red = 0; red <= maxred; red++)
 
1705
    redmap[red] = (red * 255) / maxred;
 
1706
  for (green = 0; green <= maxgreen; green++)
 
1707
    greenmap[green] = (green * 255) / maxgreen;
 
1708
  for (blue = 0; blue <= maxblue; blue++)
 
1709
    bluemap[blue] = (blue * 255) / maxblue;
 
1710
 
 
1711
  ncols = xwdhdr->l_colormap_entries;
 
1712
  if (xwdhdr->l_ncolors < ncols)
 
1713
    ncols = xwdhdr->l_ncolors;
 
1714
 
 
1715
  set_pixelmap (ncols, xwdcolmap, &pixel_map);
 
1716
 
 
1717
  /* What do we have to consume after a line has finished ? */
 
1718
  linepad =   xwdhdr->l_bytes_per_line
 
1719
    - (xwdhdr->l_pixmap_width*xwdhdr->l_bits_per_pixel)/8;
 
1720
  if (linepad < 0) linepad = 0;
 
1721
 
 
1722
  lsbyte_first = (xwdhdr->l_byte_order == 0);
 
1723
 
 
1724
  dest = data;
 
1725
  scan_lines = 0;
 
1726
 
 
1727
  if (xwdhdr->l_bits_per_pixel == 32)
 
1728
    {
 
1729
      for (i = 0; i < height; i++)
 
1730
        {
 
1731
          for (j = 0; j < width; j++)
 
1732
            {
 
1733
              c0 = getc (ifp);
 
1734
              c1 = getc (ifp);
 
1735
              c2 = getc (ifp);
 
1736
              c3 = getc (ifp);
 
1737
              if (c3 < 0)
 
1738
                {
 
1739
                  err = 1;
 
1740
                  break;
 
1741
                }
 
1742
              if (lsbyte_first)
 
1743
                pixelval = c0 | (c1 << 8) | (c2 << 16) | (c3 << 24);
 
1744
              else
 
1745
                pixelval = (c0 << 24) | (c1 << 16) | (c2 << 8) | c3;
 
1746
 
 
1747
              if (get_pixelmap (pixelval, &pixel_map, dest, dest+1, dest+2))
 
1748
                {
 
1749
                  dest += 3;
 
1750
                }
 
1751
              else
 
1752
                {
 
1753
                  *(dest++) = redmap[(pixelval & redmask) >> redshift];
 
1754
                  *(dest++) = greenmap[(pixelval & greenmask) >> greenshift];
 
1755
                  *(dest++) = bluemap[(pixelval & bluemask) >> blueshift];
 
1756
                }
 
1757
            }
 
1758
          scan_lines++;
 
1759
 
 
1760
          if (err)
 
1761
            break;
 
1762
 
 
1763
          for (j = 0; j < linepad; j++)
 
1764
            getc (ifp);
 
1765
 
 
1766
          if ((i % 20) == 0)
 
1767
            gimp_progress_update ((gdouble) (i + 1) / (gdouble) height);
 
1768
 
 
1769
          if ((scan_lines == tile_height) || ((i+1) == height))
 
1770
            {
 
1771
              gimp_pixel_rgn_set_rect (&pixel_rgn, data, 0, i-scan_lines+1,
 
1772
                                       width, scan_lines);
 
1773
              scan_lines = 0;
 
1774
              dest = data;
 
1775
            }
 
1776
        }
 
1777
    }
 
1778
  else    /* 24 bits per pixel */
 
1779
    {
 
1780
      for (i = 0; i < height; i++)
 
1781
        {
 
1782
          for (j = 0; j < width; j++)
 
1783
            {
 
1784
              c0 = getc (ifp);
 
1785
              c1 = getc (ifp);
 
1786
              c2 = getc (ifp);
 
1787
              if (c2 < 0)
 
1788
                {
 
1789
                  err = 1;
 
1790
                  break;
 
1791
                }
 
1792
              if (lsbyte_first)
 
1793
                pixelval = c0 | (c1 << 8) | (c2 << 16);
 
1794
              else
 
1795
                pixelval = (c0 << 16) | (c1 << 8) | c2;
 
1796
 
 
1797
              if (get_pixelmap (pixelval, &pixel_map, dest, dest+1, dest+2))
 
1798
                {
 
1799
                  dest += 3;
 
1800
                }
 
1801
              else
 
1802
                {
 
1803
                  *(dest++) = redmap[(pixelval & redmask) >> redshift];
 
1804
                  *(dest++) = greenmap[(pixelval & greenmask) >> greenshift];
 
1805
                  *(dest++) = bluemap[(pixelval & bluemask) >> blueshift];
 
1806
                }
 
1807
            }
 
1808
          scan_lines++;
 
1809
 
 
1810
          if (err)
 
1811
            break;
 
1812
 
 
1813
          for (j = 0; j < linepad; j++)
 
1814
            getc (ifp);
 
1815
 
 
1816
          if ((i % 20) == 0)
 
1817
            gimp_progress_update ((gdouble) (i + 1) / (gdouble) height);
 
1818
 
 
1819
          if ((scan_lines == tile_height) || ((i+1) == height))
 
1820
            {
 
1821
              gimp_pixel_rgn_set_rect (&pixel_rgn, data, 0, i-scan_lines+1,
 
1822
                                       width, scan_lines);
 
1823
              scan_lines = 0;
 
1824
              dest = data;
 
1825
            }
 
1826
        }
 
1827
    }
 
1828
 
 
1829
  g_free (data);
 
1830
 
 
1831
  if (err)
 
1832
    g_message (_("EOF encountered on reading"));
 
1833
 
 
1834
  gimp_drawable_flush (drawable);
 
1835
 
 
1836
  return err ? -1 : image_ID;
 
1837
}
 
1838
 
 
1839
 
 
1840
/* Load XWD with pixmap_format 1, pixmap_depth up to 24, bits_per_pixel 1 */
 
1841
 
 
1842
static gint32
 
1843
load_xwd_f1_d24_b1 (const gchar      *filename,
 
1844
                    FILE             *ifp,
 
1845
                    L_XWDFILEHEADER  *xwdhdr,
 
1846
                    L_XWDCOLOR       *xwdcolmap,
 
1847
                    GError          **error)
 
1848
{
 
1849
  register guchar *dest, outmask, inmask, do_reverse;
 
1850
  gint             width, height, i, j, plane, fromright;
 
1851
  gint             tile_height, tile_start, tile_end;
 
1852
  gint             indexed, bytes_per_pixel;
 
1853
  gint             maxred, maxgreen, maxblue;
 
1854
  gint             red, green, blue, ncols, standard_rgb;
 
1855
  glong            data_offset, plane_offset, tile_offset;
 
1856
  gulong           redmask, greenmask, bluemask;
 
1857
  guint            redshift, greenshift, blueshift;
 
1858
  gulong           g;
 
1859
  guchar           redmap[256], greenmap[256], bluemap[256];
 
1860
  guchar           bit_reverse[256];
 
1861
  guchar          *xwddata, *xwdin, *data;
 
1862
  L_CARD32         pixelval;
 
1863
  PIXEL_MAP        pixel_map;
 
1864
  gint             err = 0;
 
1865
  gint32           layer_ID, image_ID;
 
1866
  GimpPixelRgn     pixel_rgn;
 
1867
  GimpDrawable    *drawable;
 
1868
 
 
1869
#ifdef XWD_DEBUG
 
1870
  printf ("load_xwd_f1_d24_b1 (%s)\n", filename);
 
1871
#endif
 
1872
 
 
1873
  xwddata = g_malloc (xwdhdr->l_bytes_per_line);
 
1874
  if (xwddata == NULL)
 
1875
    return -1;
 
1876
 
 
1877
  width           = xwdhdr->l_pixmap_width;
 
1878
  height          = xwdhdr->l_pixmap_height;
 
1879
  indexed         = (xwdhdr->l_pixmap_depth <= 8);
 
1880
  bytes_per_pixel = (indexed ? 1 : 3);
 
1881
 
 
1882
  for (j = 0; j < 256; j++)   /* Create an array for reversing bits */
 
1883
    {
 
1884
      inmask = 0;
 
1885
      for (i = 0; i < 8; i++)
 
1886
        {
 
1887
          inmask <<= 1;
 
1888
          if (j & (1 << i)) inmask |= 1;
 
1889
        }
 
1890
      bit_reverse[j] = inmask;
 
1891
    }
 
1892
 
 
1893
  redmask   = xwdhdr->l_red_mask;
 
1894
  greenmask = xwdhdr->l_green_mask;
 
1895
  bluemask  = xwdhdr->l_blue_mask;
 
1896
 
 
1897
  if (redmask   == 0) redmask   = 0xff0000;
 
1898
  if (greenmask == 0) greenmask = 0x00ff00;
 
1899
  if (bluemask  == 0) bluemask  = 0x0000ff;
 
1900
 
 
1901
  standard_rgb =    (redmask == 0xff0000) && (greenmask == 0x00ff00)
 
1902
    && (bluemask == 0x0000ff);
 
1903
  redshift = greenshift = blueshift = 0;
 
1904
 
 
1905
  if (!standard_rgb)   /* Do we need to re-map the pixel-values ? */
 
1906
    {
 
1907
      /* How to shift RGB to be right aligned ? */
 
1908
      /* (We rely on the the mask bits are grouped and not mixed) */
 
1909
 
 
1910
      while (((1 << redshift)   & redmask)   == 0) redshift++;
 
1911
      while (((1 << greenshift) & greenmask) == 0) greenshift++;
 
1912
      while (((1 << blueshift)  & bluemask)  == 0) blueshift++;
 
1913
 
 
1914
      /* The bits_per_rgb may not be correct. Use redmask instead */
 
1915
 
 
1916
      maxred = 0; while (redmask >> (redshift + maxred)) maxred++;
 
1917
      maxred = (1 << maxred) - 1;
 
1918
 
 
1919
      maxgreen = 0; while (greenmask >> (greenshift + maxgreen)) maxgreen++;
 
1920
      maxgreen = (1 << maxgreen) - 1;
 
1921
 
 
1922
      maxblue = 0; while (bluemask >> (blueshift + maxblue)) maxblue++;
 
1923
      maxblue = (1 << maxblue) - 1;
 
1924
 
 
1925
      if (maxred   > sizeof (redmap)   ||
 
1926
          maxgreen > sizeof (greenmap) ||
 
1927
          maxblue  > sizeof (bluemap))
 
1928
        {
 
1929
          g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
 
1930
                       _("XWD-file %s is corrupt."),
 
1931
                       gimp_filename_to_utf8 (filename));
 
1932
          return -1;
 
1933
        }
 
1934
 
 
1935
      /* Set map-arrays for red, green, blue */
 
1936
      for (red = 0; red <= maxred; red++)
 
1937
        redmap[red] = (red * 255) / maxred;
 
1938
      for (green = 0; green <= maxgreen; green++)
 
1939
        greenmap[green] = (green * 255) / maxgreen;
 
1940
      for (blue = 0; blue <= maxblue; blue++)
 
1941
        bluemap[blue] = (blue * 255) / maxblue;
 
1942
    }
 
1943
 
 
1944
  image_ID = create_new_image (filename, width, height,
 
1945
                               indexed ? GIMP_INDEXED : GIMP_RGB,
 
1946
                               &layer_ID, &drawable, &pixel_rgn);
 
1947
 
 
1948
  tile_height = gimp_tile_height ();
 
1949
  data = g_malloc (tile_height * width * bytes_per_pixel);
 
1950
 
 
1951
  ncols = xwdhdr->l_colormap_entries;
 
1952
  if (xwdhdr->l_ncolors < ncols)
 
1953
    ncols = xwdhdr->l_ncolors;
 
1954
 
 
1955
  if (indexed)
 
1956
    {
 
1957
      if (ncols < 2)
 
1958
        set_bw_color_table (image_ID);
 
1959
      else
 
1960
        set_color_table (image_ID, xwdhdr, xwdcolmap);
 
1961
    }
 
1962
  else
 
1963
    {
 
1964
      set_pixelmap (ncols, xwdcolmap, &pixel_map);
 
1965
    }
 
1966
 
 
1967
  do_reverse = !xwdhdr->l_bitmap_bit_order;
 
1968
 
 
1969
  /* This is where the image data starts within the file */
 
1970
  data_offset = ftell (ifp);
 
1971
 
 
1972
  for (tile_start = 0; tile_start < height; tile_start += tile_height)
 
1973
    {
 
1974
      memset (data, 0, width*tile_height*bytes_per_pixel);
 
1975
 
 
1976
      tile_end = tile_start + tile_height - 1;
 
1977
      if (tile_end >= height)
 
1978
        tile_end = height - 1;
 
1979
 
 
1980
      for (plane = 0; plane < xwdhdr->l_pixmap_depth; plane++)
 
1981
        {
 
1982
          dest = data;    /* Position to start of tile within the plane */
 
1983
          plane_offset = data_offset + plane*height*xwdhdr->l_bytes_per_line;
 
1984
          tile_offset = plane_offset + tile_start*xwdhdr->l_bytes_per_line;
 
1985
          fseek (ifp, tile_offset, SEEK_SET);
 
1986
 
 
1987
          /* Place the last plane at the least significant bit */
 
1988
 
 
1989
          if (indexed)   /* Only 1 byte per pixel */
 
1990
            {
 
1991
              fromright = xwdhdr->l_pixmap_depth-1-plane;
 
1992
              outmask = (1 << fromright);
 
1993
            }
 
1994
          else           /* 3 bytes per pixel */
 
1995
            {
 
1996
              fromright = xwdhdr->l_pixmap_depth-1-plane;
 
1997
              dest += 2 - fromright/8;
 
1998
              outmask = (1 << (fromright % 8));
 
1999
            }
 
2000
 
 
2001
          for (i = tile_start; i <= tile_end; i++)
 
2002
            {
 
2003
              if (fread (xwddata,xwdhdr->l_bytes_per_line,1,ifp) != 1)
 
2004
                {
 
2005
                  err = 1;
 
2006
                  break;
 
2007
                }
 
2008
              xwdin = xwddata;
 
2009
 
 
2010
              /* Handle bitmap unit */
 
2011
              if (xwdhdr->l_bitmap_unit == 16)
 
2012
                {
 
2013
                  if (xwdhdr->l_bitmap_bit_order != xwdhdr->l_byte_order)
 
2014
                    {
 
2015
                      j = xwdhdr->l_bytes_per_line/2;
 
2016
                      while (j--)
 
2017
                        {
 
2018
                          inmask = xwdin[0]; xwdin[0] = xwdin[1]; xwdin[1] = inmask;
 
2019
                          xwdin += 2;
 
2020
                        }
 
2021
                      xwdin = xwddata;
 
2022
                    }
 
2023
                }
 
2024
              else if (xwdhdr->l_bitmap_unit == 32)
 
2025
                {
 
2026
                  if (xwdhdr->l_bitmap_bit_order != xwdhdr->l_byte_order)
 
2027
                    {
 
2028
                      j = xwdhdr->l_bytes_per_line/4;
 
2029
                      while (j--)
 
2030
                        {
 
2031
                          inmask = xwdin[0]; xwdin[0] = xwdin[3]; xwdin[3] = inmask;
 
2032
                          inmask = xwdin[1]; xwdin[1] = xwdin[2]; xwdin[2] = inmask;
 
2033
                          xwdin += 4;
 
2034
                        }
 
2035
                      xwdin = xwddata;
 
2036
                    }
 
2037
                }
 
2038
 
 
2039
              g = inmask = 0;
 
2040
              for (j = 0; j < width; j++)
 
2041
                {
 
2042
                  if (!inmask)
 
2043
                    {
 
2044
                      g = *(xwdin++);
 
2045
                      if (do_reverse)
 
2046
                        g = bit_reverse[g];
 
2047
                      inmask = 0x80;
 
2048
                    }
 
2049
 
 
2050
                  if (g & inmask)
 
2051
                    *dest |= outmask;
 
2052
                  dest += bytes_per_pixel;
 
2053
 
 
2054
                  inmask >>= 1;
 
2055
                }
 
2056
            }
 
2057
        }
 
2058
 
 
2059
      /* For indexed images, the mapping to colors is done by the color table. */
 
2060
      /* Otherwise we must do the mapping by ourself. */
 
2061
      if (!indexed)
 
2062
        {
 
2063
          dest = data;
 
2064
          for (i = tile_start; i <= tile_end; i++)
 
2065
            {
 
2066
              for (j = 0; j < width; j++)
 
2067
                {
 
2068
                  pixelval = (*dest << 16) | (*(dest+1) << 8) | *(dest+2);
 
2069
 
 
2070
                  if (get_pixelmap (pixelval, &pixel_map, dest, dest+1, dest+2)
 
2071
                      || standard_rgb)
 
2072
                    {
 
2073
                      dest += 3;
 
2074
                    }
 
2075
                  else   /* We have to map RGB to 0,...,255 */
 
2076
                    {
 
2077
                      *(dest++) = redmap[(pixelval & redmask) >> redshift];
 
2078
                      *(dest++) = greenmap[(pixelval & greenmask) >> greenshift];
 
2079
                      *(dest++) = bluemap[(pixelval & bluemask) >> blueshift];
 
2080
                    }
 
2081
                }
 
2082
            }
 
2083
        }
 
2084
 
 
2085
      gimp_progress_update ((gdouble) tile_end / (gdouble) height);
 
2086
 
 
2087
      gimp_pixel_rgn_set_rect (&pixel_rgn, data, 0, tile_start,
 
2088
                               width, tile_end-tile_start+1);
 
2089
    }
 
2090
 
 
2091
  g_free (data);
 
2092
  g_free (xwddata);
 
2093
 
 
2094
  if (err)
 
2095
    g_message (_("EOF encountered on reading"));
 
2096
 
 
2097
  gimp_drawable_flush (drawable);
 
2098
 
 
2099
  return err ? -1 : image_ID;
 
2100
}
 
2101
 
 
2102
 
 
2103
static gint
 
2104
save_index (FILE    *ofp,
 
2105
            gint32   image_ID,
 
2106
            gint32   drawable_ID,
 
2107
            gint     gray)
 
2108
{
 
2109
  gint             height, width, linepad, tile_height, i, j;
 
2110
  gint             ncolors, vclass;
 
2111
  glong            tmp = 0;
 
2112
  guchar          *data, *src, *cmap;
 
2113
  L_XWDFILEHEADER  xwdhdr;
 
2114
  L_XWDCOLOR       xwdcolmap[256];
 
2115
  GimpPixelRgn     pixel_rgn;
 
2116
  GimpDrawable    *drawable;
 
2117
 
 
2118
#ifdef XWD_DEBUG
 
2119
  printf ("save_index ()\n");
 
2120
#endif
 
2121
 
 
2122
  drawable      = gimp_drawable_get (drawable_ID);
 
2123
  width         = drawable->width;
 
2124
  height        = drawable->height;
 
2125
  tile_height   = gimp_tile_height ();
 
2126
 
 
2127
  gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0, width, height, FALSE, FALSE);
 
2128
 
 
2129
  /* allocate a buffer for retrieving information from the pixel region  */
 
2130
  src = data = g_new (guchar, tile_height * width * drawable->bpp);
 
2131
 
 
2132
  linepad = width % 4;
 
2133
  if (linepad) linepad = 4 - linepad;
 
2134
 
 
2135
  /* Fill XWD-color map */
 
2136
  if (gray)
 
2137
    {
 
2138
      vclass = 0;
 
2139
      ncolors = 256;
 
2140
 
 
2141
      for (j = 0; j < ncolors; j++)
 
2142
        {
 
2143
          xwdcolmap[j].l_pixel = j;
 
2144
          xwdcolmap[j].l_red   = (j << 8) | j;
 
2145
          xwdcolmap[j].l_green = (j << 8) | j;
 
2146
          xwdcolmap[j].l_blue  = (j << 8) | j;
 
2147
          xwdcolmap[j].l_flags = 7;
 
2148
          xwdcolmap[j].l_pad = 0;
 
2149
        }
 
2150
    }
 
2151
  else
 
2152
    {
 
2153
      vclass = 3;
 
2154
      cmap = gimp_image_get_colormap (image_ID, &ncolors);
 
2155
 
 
2156
      for (j = 0; j < ncolors; j++)
 
2157
        {
 
2158
          xwdcolmap[j].l_pixel = j;
 
2159
          xwdcolmap[j].l_red   = ((*cmap) << 8) | *cmap; cmap++;
 
2160
          xwdcolmap[j].l_green = ((*cmap) << 8) | *cmap; cmap++;
 
2161
          xwdcolmap[j].l_blue  = ((*cmap) << 8) | *cmap; cmap++;
 
2162
          xwdcolmap[j].l_flags = 7;
 
2163
          xwdcolmap[j].l_pad = 0;
 
2164
        }
 
2165
    }
 
2166
 
 
2167
  /* Fill in the XWD header (header_size is evaluated by write_xwd_hdr ()) */
 
2168
  xwdhdr.l_header_size      = 0;
 
2169
  xwdhdr.l_file_version     = 7;
 
2170
  xwdhdr.l_pixmap_format    = 2;
 
2171
  xwdhdr.l_pixmap_depth     = 8;
 
2172
  xwdhdr.l_pixmap_width     = width;
 
2173
  xwdhdr.l_pixmap_height    = height;
 
2174
  xwdhdr.l_xoffset          = 0;
 
2175
  xwdhdr.l_byte_order       = 1;
 
2176
  xwdhdr.l_bitmap_unit      = 32;
 
2177
  xwdhdr.l_bitmap_bit_order = 1;
 
2178
  xwdhdr.l_bitmap_pad       = 32;
 
2179
  xwdhdr.l_bits_per_pixel   = 8;
 
2180
  xwdhdr.l_bytes_per_line   = width + linepad;
 
2181
  xwdhdr.l_visual_class     = vclass;
 
2182
  xwdhdr.l_red_mask         = 0x000000;
 
2183
  xwdhdr.l_green_mask       = 0x000000;
 
2184
  xwdhdr.l_blue_mask        = 0x000000;
 
2185
  xwdhdr.l_bits_per_rgb     = 8;
 
2186
  xwdhdr.l_colormap_entries = ncolors;
 
2187
  xwdhdr.l_ncolors          = ncolors;
 
2188
  xwdhdr.l_window_width     = width;
 
2189
  xwdhdr.l_window_height    = height;
 
2190
  xwdhdr.l_window_x         = 64;
 
2191
  xwdhdr.l_window_y         = 64;
 
2192
  xwdhdr.l_window_bdrwidth  = 0;
 
2193
 
 
2194
  write_xwd_header (ofp, &xwdhdr);
 
2195
  write_xwd_cols (ofp, &xwdhdr, xwdcolmap);
 
2196
 
 
2197
  for (i = 0; i < height; i++)
 
2198
    {
 
2199
      if ((i % tile_height) == 0)   /* Get more data */
 
2200
        {
 
2201
          gint scan_lines = (i + tile_height - 1 < height) ? tile_height : (height - i);
 
2202
 
 
2203
          gimp_pixel_rgn_get_rect (&pixel_rgn, data, 0, i, width, scan_lines);
 
2204
          src = data;
 
2205
        }
 
2206
 
 
2207
      fwrite (src, width, 1, ofp);
 
2208
 
 
2209
      if (linepad)
 
2210
        fwrite ((char *)&tmp, linepad, 1, ofp);
 
2211
 
 
2212
      src += width;
 
2213
 
 
2214
      if ((i % 20) == 0)
 
2215
        gimp_progress_update ((gdouble) i / (gdouble) height);
 
2216
    }
 
2217
 
 
2218
  g_free (data);
 
2219
 
 
2220
  gimp_drawable_detach (drawable);
 
2221
 
 
2222
  if (ferror (ofp))
 
2223
    {
 
2224
      g_message (_("Error during writing indexed/gray image"));
 
2225
      return FALSE;
 
2226
    }
 
2227
 
 
2228
  return TRUE;
 
2229
}
 
2230
 
 
2231
 
 
2232
static gint
 
2233
save_rgb (FILE   *ofp,
 
2234
          gint32  image_ID,
 
2235
          gint32  drawable_ID)
 
2236
{
 
2237
  gint             height, width, linepad, tile_height, i;
 
2238
  glong            tmp = 0;
 
2239
  guchar          *data, *src;
 
2240
  L_XWDFILEHEADER  xwdhdr;
 
2241
  GimpPixelRgn     pixel_rgn;
 
2242
  GimpDrawable    *drawable;
 
2243
 
 
2244
#ifdef XWD_DEBUG
 
2245
  printf ("save_rgb ()\n");
 
2246
#endif
 
2247
 
 
2248
  drawable      = gimp_drawable_get (drawable_ID);
 
2249
  width         = drawable->width;
 
2250
  height        = drawable->height;
 
2251
  tile_height   = gimp_tile_height ();
 
2252
 
 
2253
  gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0, width, height, FALSE, FALSE);
 
2254
 
 
2255
  /* allocate a buffer for retrieving information from the pixel region  */
 
2256
  src = data = g_new (guchar, tile_height * width * drawable->bpp);
 
2257
 
 
2258
  linepad = (width * 3) % 4;
 
2259
  if (linepad)
 
2260
    linepad = 4 - linepad;
 
2261
 
 
2262
  /* Fill in the XWD header (header_size is evaluated by write_xwd_hdr ()) */
 
2263
  xwdhdr.l_header_size      = 0;
 
2264
  xwdhdr.l_file_version     = 7;
 
2265
  xwdhdr.l_pixmap_format    = 2;
 
2266
  xwdhdr.l_pixmap_depth     = 24;
 
2267
  xwdhdr.l_pixmap_width     = width;
 
2268
  xwdhdr.l_pixmap_height    = height;
 
2269
  xwdhdr.l_xoffset          = 0;
 
2270
  xwdhdr.l_byte_order       = 1;
 
2271
 
 
2272
  xwdhdr.l_bitmap_unit      = 32;
 
2273
  xwdhdr.l_bitmap_bit_order = 1;
 
2274
  xwdhdr.l_bitmap_pad       = 32;
 
2275
  xwdhdr.l_bits_per_pixel   = 24;
 
2276
 
 
2277
  xwdhdr.l_bytes_per_line   = width * 3 + linepad;
 
2278
  xwdhdr.l_visual_class     = 5;
 
2279
  xwdhdr.l_red_mask         = 0xff0000;
 
2280
  xwdhdr.l_green_mask       = 0x00ff00;
 
2281
  xwdhdr.l_blue_mask        = 0x0000ff;
 
2282
  xwdhdr.l_bits_per_rgb     = 8;
 
2283
  xwdhdr.l_colormap_entries = 0;
 
2284
  xwdhdr.l_ncolors          = 0;
 
2285
  xwdhdr.l_window_width     = width;
 
2286
  xwdhdr.l_window_height    = height;
 
2287
  xwdhdr.l_window_x         = 64;
 
2288
  xwdhdr.l_window_y         = 64;
 
2289
  xwdhdr.l_window_bdrwidth  = 0;
 
2290
 
 
2291
  write_xwd_header (ofp, &xwdhdr);
 
2292
 
 
2293
  for (i = 0; i < height; i++)
 
2294
    {
 
2295
      if ((i % tile_height) == 0)   /* Get more data */
 
2296
        {
 
2297
          gint scan_lines = (i + tile_height - 1 < height) ? tile_height : (height - i);
 
2298
 
 
2299
          gimp_pixel_rgn_get_rect (&pixel_rgn, data, 0, i, width, scan_lines);
 
2300
          src = data;
 
2301
        }
 
2302
 
 
2303
      fwrite (src, width * 3, 1, ofp);
 
2304
 
 
2305
      if (linepad)
 
2306
        fwrite ((char *)&tmp, linepad, 1, ofp);
 
2307
 
 
2308
      src += width * 3;
 
2309
 
 
2310
      if ((i % 20) == 0)
 
2311
        gimp_progress_update ((gdouble) i / (gdouble) height);
 
2312
    }
 
2313
 
 
2314
  g_free (data);
 
2315
 
 
2316
  gimp_drawable_detach (drawable);
 
2317
 
 
2318
  if (ferror (ofp))
 
2319
    {
 
2320
      g_message (_("Error during writing rgb image"));
 
2321
      return FALSE;
 
2322
    }
 
2323
 
 
2324
  return TRUE;
 
2325
}