~ubuntu-branches/ubuntu/maverick/gimp/maverick-updates

« back to all changes in this revision

Viewing changes to plug-ins/common/psd.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-12-09 19:44:52 UTC
  • Revision ID: james.westby@ubuntu.com-20051209194452-yggpemjlofpjqyf4
Tags: upstream-2.2.9
ImportĀ upstreamĀ versionĀ 2.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * PSD Plugin version 3.0.14
 
3
 * This GIMP plug-in is designed to load Adobe Photoshop(tm) files (.PSD)
 
4
 *
 
5
 * Adam D. Moss <adam@gimp.org> <adam@foxbox.org>
 
6
 *
 
7
 *     If this plug-in fails to load a file which you think it should,
 
8
 *     please file a Bugzilla bug describing what seemed to go wrong,
 
9
 *     and anything you know about the image you tried to load.  Attach a
 
10
 *     problematic PSD file to the bug report.
 
11
 *
 
12
 *          Copyright (C) 1997-2004 Adam D. Moss
 
13
 *          Copyright (C) 1996      Torsten Martinsen
 
14
 *
 
15
 * This program is free software; you can redistribute it and/or modify
 
16
 * it under the terms of the GNU General Public License as published by
 
17
 * the Free Software Foundation; either version 2 of the License, or
 
18
 * (at your option) any later version.
 
19
 *
 
20
 * This program is distributed in the hope that it will be useful,
 
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
23
 * GNU General Public License for more details.
 
24
 *
 
25
 * You should have received a copy of the GNU General Public License
 
26
 * along with this program; if not, write to the Free Software
 
27
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
28
 */
 
29
 
 
30
/*
 
31
 * Adobe and Adobe Photoshop are trademarks of Adobe Systems
 
32
 * Incorporated that may be registered in certain jurisdictions.
 
33
 */
 
34
 
 
35
/*
 
36
 * Revision history:
 
37
 *
 
38
 *  2004-02-12 / v3.0.14 / Adam D. Moss
 
39
 *       Fix a twisted utf8-obsessive bug diagnosed by
 
40
 *       Piotr Krysiuk <KrysiukP@prokom.pl>
 
41
 *
 
42
 *  2004-01-06 / v3.0.13 / Adam D. Moss
 
43
 *       Disable one of the PanoTools fixes by default, since it causes
 
44
 *       regressions in some ordinary PSD file loading.
 
45
 *
 
46
 *  2004-01-06 / v3.0.12 / Adam D. Moss
 
47
 *       Try to avoid 0-sized drawables (including channels) in all
 
48
 *       circumstances under GIMP 2.
 
49
 *
 
50
 *  2004-01-01 / v3.0.11 / Daniel Rogers <dsrogers@phaseveloctiy.org>
 
51
 *       GIMP crashes on 0x0 layers, so we skip them.
 
52
 *
 
53
 *  2003-11-27 / v3.0.10 / Adam D. Moss
 
54
 *       GIMP 1.3/2.0 needs its layer/channel names to be UTF8 or it
 
55
 *       fails wackily, so convert the strings from the PSD file to
 
56
 *       UTF8 instead of using them raw.
 
57
 *
 
58
 *  2003-10-05 / v3.0.9 / Morten Eriksen
 
59
 *       Fixed memory corruption bug: too little memory was allocated
 
60
 *       for the bitmap image buffer if (imgwidth % 8 != 0) for
 
61
 *       monocolor PSD images.
 
62
 *
 
63
 *  2003-08-31 / v3.0.8 / applied (modified) patch from Andy Wallis
 
64
 *       Fix for handling of layer masks. See bug #68538.
 
65
 *
 
66
 *  2003-06-16 / v3.0.7 / Adam D. Moss
 
67
 *       Avoid memory corruption when things get shot to hell in the
 
68
 *       image unpacking phase.  Major version bumped to distinguish
 
69
 *       GIMP 1.3 development thread.
 
70
 *
 
71
 *  2000-08-23 / v2.0.6 / Adam D. Moss
 
72
 *       Eliminate more debugging output (don't people have more
 
73
 *       substantial problems to report?  I'm poised at my keyboard).
 
74
 *
 
75
 *  1999-11-14 / v2.0.5 / Adam D. Moss
 
76
 *       Applied patch by Andy Hefner to load 1-bit images.
 
77
 *
 
78
 *  1999-08-13 / v2.0.4 / Adam D. Moss
 
79
 *       Allowed NULL layer names again, whee.  (Also fixed the time machine.)
 
80
 *
 
81
 *  1999-08-20 / v2.0.3 / Adam D. Moss
 
82
 *       Ensure that NULL name does not get passed to gimp_layer_new(),
 
83
 *       or it will fail to create the layer and cause problems down
 
84
 *       the line (only since April 1999).
 
85
 *
 
86
 *  1999-01-18 / v2.0.2 / Adam D. Moss
 
87
 *       Better guess at how PSD files store Guide position precision.
 
88
 *
 
89
 *  1999-01-10 / v2.0.1 / Adam D. Moss
 
90
 *       Greatly reduced memory requirements for layered image loading -
 
91
 *       we now do just-in-time channel unpacking.  Some little
 
92
 *       cleanups too.
 
93
 *
 
94
 *  1998-09-04 / v2.0.0 / Adam D. Moss
 
95
 *       Now recognises and loads the new Guides extensions written
 
96
 *       by Photoshop 4 and 5.
 
97
 *
 
98
 *  1998-07-31 / v1.9.9.9f / Adam D. Moss
 
99
 *       Use GIMP_OVERLAY_MODE if available.
 
100
 *
 
101
 *  1998-07-31 / v1.9.9.9e / Adam D. Moss
 
102
 *       Worked around some buggy PSD savers (suspect PS4 on Mac) - ugh.
 
103
 *       Fixed a bug when loading layer masks of certain dimensions.
 
104
 *
 
105
 *  1998-05-04 / v1.9.9.9b / Adam D. Moss
 
106
 *       Changed the Pascal-style string-reading stuff.  That fixed
 
107
 *       some file-padding problems.  Made all debugging output
 
108
 *       compile-time optional (please leave it enabled for now).
 
109
 *       Reduced memory requirements; still much room for improvement.
 
110
 *
 
111
 *  1998-04-28 / v1.9.9.9 / Adam D. Moss
 
112
 *       Fixed the correct channel interlacing of 'raw' flat images.
 
113
 *       Thanks to Christian Kirsch and Jay Cox for spotting this.
 
114
 *       Changed some of the I/O routines.
 
115
 *
 
116
 *  1998-04-26 / v1.9.9.8 / Adam D. Moss
 
117
 *       Implemented Aux-channels for layered files.  Got rid
 
118
 *       of <endian.h> nonsense.  Improved Layer Mask padding.
 
119
 *       Enforced num_layers/num_channels limit checks.
 
120
 *
 
121
 *  1998-04-23 / v1.9.9.5 / Adam D. Moss
 
122
 *       Got Layer Masks working, got Aux-channels working
 
123
 *       for unlayered files, fixed 'raw' channel loading, fixed
 
124
 *       some other mini-bugs, slightly better progress meters.
 
125
 *       Thanks to everyone who is helping with the testing!
 
126
 *
 
127
 *  1998-04-21 / v1.9.9.1 / Adam D. Moss
 
128
 *       A little cleanup.  Implemented Layer Masks but disabled
 
129
 *       them again - PS masks can be a different size to their
 
130
 *       owning layer, unlike those in GIMP.
 
131
 *
 
132
 *  1998-04-19 / v1.9.9.0 / Adam D. Moss
 
133
 *       Much happier now.
 
134
 *
 
135
 *  1997-03-13 / v1.9.0 / Adam D. Moss
 
136
 *       Layers, channels and masks, oh my.
 
137
 *       + Bugfixes & rearchitecturing.
 
138
 *
 
139
 *  1997-01-30 / v1.0.12 / Torsten Martinsen
 
140
 *       Flat PSD image loading.
 
141
 */
 
142
 
 
143
/*
 
144
 * TODO:
 
145
 *
 
146
 *      Crush 16bpp channels *
 
147
 *      CMYK -> RGB *
 
148
 *      * I don't think these should be done lossily -- wait for
 
149
 *        GIMP to be able to support them natively.
 
150
 *
 
151
 *      Read in the paths.
 
152
 *
 
153
 *      File saving (someone has an alpha plugin for this)
 
154
 */
 
155
 
 
156
/*
 
157
 * BUGS:
 
158
 *
 
159
 *      Sometimes creates a superfluous aux channel?  Harmless.
 
160
 */
 
161
 
 
162
 
 
163
 
 
164
/* *** USER DEFINES *** */
 
165
 
 
166
/* set to TRUE if you want debugging, FALSE otherwise */
 
167
#define PSD_DEBUG FALSE
 
168
 
 
169
/* the max number of channels that this plugin should let a layer have */
 
170
#define MAX_CHANNELS 30
 
171
 
 
172
/* set to TRUE to allow a fix for transparency in PSD files
 
173
   generated by PanoTools that unfortunately causes regressions
 
174
   in some other ordinary files saved by Photoshop. */
 
175
#define PANOTOOLS_FIX FALSE
 
176
 
 
177
/* *** END OF USER DEFINES *** */
 
178
 
 
179
 
 
180
 
 
181
#define IFDBG if (PSD_DEBUG)
 
182
 
 
183
#include "config.h"
 
184
 
 
185
#include <errno.h>
 
186
#include <stdio.h>
 
187
#include <stdlib.h>
 
188
#ifdef HAVE_UNISTD_H
 
189
#include <unistd.h>
 
190
#endif
 
191
#include <string.h>
 
192
 
 
193
#include <glib.h>
 
194
 
 
195
#include <libgimp/gimp.h>
 
196
 
 
197
#include "libgimp/stdplugins-intl.h"
 
198
 
 
199
 
 
200
/* Local types etc
 
201
 */
 
202
typedef enum
 
203
{
 
204
  PSD_UNKNOWN_IMAGE,
 
205
  PSD_RGB_IMAGE,
 
206
  PSD_RGBA_IMAGE,
 
207
  PSD_GRAY_IMAGE,
 
208
  PSD_GRAYA_IMAGE,
 
209
  PSD_INDEXED_IMAGE,
 
210
  PSD_INDEXEDA_IMAGE,
 
211
  PSD_BITMAP_IMAGE
 
212
} psd_imagetype;
 
213
 
 
214
 
 
215
typedef struct PsdChannel
 
216
{
 
217
  gchar *name;
 
218
  guchar *data;
 
219
  gint type;
 
220
 
 
221
  guint32 compressedsize;
 
222
 
 
223
  fpos_t fpos; /* Remember where the data is in the file, so we can
 
224
                  come back to it! */
 
225
 
 
226
 /* We can't just assume that the channel's width and height are the
 
227
  * same as those of the layer that owns the channel, since this
 
228
  * channel may be a layer mask, which Photoshop allows to have a
 
229
  * different size from the layer which it applies to.
 
230
  */
 
231
  guint32 width;
 
232
  guint32 height;
 
233
 
 
234
} PSDchannel;
 
235
 
 
236
typedef struct PsdGuide
 
237
{
 
238
  gboolean horizontal; /* else vertical */
 
239
  gint position;
 
240
} PSDguide;
 
241
 
 
242
typedef struct PsdLayer
 
243
{
 
244
  gint num_channels;
 
245
  PSDchannel *channel;
 
246
 
 
247
  gint32 x;
 
248
  gint32 y;
 
249
  guint32 width;
 
250
  guint32 height;
 
251
 
 
252
  gchar blendkey[4];
 
253
  guchar opacity;
 
254
  gchar clipping;
 
255
 
 
256
  gboolean protecttrans;
 
257
  gboolean visible;
 
258
 
 
259
  gchar* name;
 
260
 
 
261
  gint32 lm_x;
 
262
  gint32 lm_y;
 
263
  gint32 lm_width;
 
264
  gint32 lm_height;
 
265
 
 
266
} PSDlayer;
 
267
 
 
268
 
 
269
typedef gint32 Fixed;
 
270
 
 
271
typedef struct
 
272
{
 
273
  Fixed hRes;
 
274
  gint16 hRes_unit;
 
275
  gint16 widthUnit;
 
276
 
 
277
  Fixed vRes;
 
278
  gint16 vRes_unit;
 
279
  gint16 heightUnit;
 
280
 
 
281
/* Res_unit :
 
282
        1 == Pixels per inch
 
283
        2 == Pixels per cm
 
284
*/
 
285
 
 
286
} PSDresolution;
 
287
 
 
288
typedef struct PsdImage
 
289
{
 
290
  gint num_layers;
 
291
  PSDlayer *layer;
 
292
 
 
293
  gboolean absolute_alpha;
 
294
 
 
295
  gint type;
 
296
 
 
297
  gulong colmaplen;
 
298
  guchar *colmapdata;
 
299
 
 
300
  guint num_aux_channels;
 
301
  PSDchannel aux_channel[MAX_CHANNELS];
 
302
 
 
303
  guint num_guides;
 
304
  PSDguide *guides;
 
305
 
 
306
  gchar *caption;
 
307
 
 
308
  guint active_layer_num;
 
309
 
 
310
  guint resolution_is_set;
 
311
  PSDresolution resolution;
 
312
} PSDimage;
 
313
 
 
314
/* Declare some local functions.
 
315
 */
 
316
static void   query      (void);
 
317
static void   run        (const gchar      *name,
 
318
                          gint              nparams,
 
319
                          const GimpParam  *param,
 
320
                          gint             *nreturn_vals,
 
321
                          GimpParam       **return_vals);
 
322
 
 
323
static GimpImageType  psd_type_to_gimp_type      (psd_imagetype  psdtype);
 
324
static GimpImageBaseType     psd_type_to_gimp_base_type (psd_imagetype  psdtype);
 
325
static GimpLayerModeEffects     psd_lmode_to_gimp_lmode    (gchar          modekey[4]);
 
326
static GimpUnit       psd_unit_to_gimp_unit      (gint           psdunit);
 
327
 
 
328
static gint32         load_image                 (const gchar  *filename);
 
329
 
 
330
 
 
331
 
 
332
/* Various local variables...
 
333
 */
 
334
GimpPlugInInfo PLUG_IN_INFO =
 
335
{
 
336
  NULL,  /* init_proc  */
 
337
  NULL,  /* quit_proc  */
 
338
  query, /* query_proc */
 
339
  run,   /* run_proc   */
 
340
};
 
341
 
 
342
 
 
343
static PSDimage psd_image;
 
344
 
 
345
 
 
346
static struct
 
347
{
 
348
  gchar    signature[4];
 
349
  gushort  version;
 
350
  guchar   reserved[6];
 
351
  gushort  channels;
 
352
  gulong   rows;
 
353
  gulong   columns;
 
354
  gushort  bpp;
 
355
  gushort  mode;
 
356
  gulong   imgreslen;
 
357
  gulong   miscsizelen;
 
358
  gushort  compression;
 
359
  gushort *rowlength;
 
360
  long     imgdatalen;
 
361
} PSDheader;
 
362
 
 
363
 
 
364
static gchar * modename[] =
 
365
{
 
366
  "Bitmap",
 
367
  "Grayscale",
 
368
  "Indexed Colour",
 
369
  "RGB Colour",
 
370
  "CMYK Colour",
 
371
  "<invalid>",
 
372
  "<invalid>",
 
373
  "Multichannel",
 
374
  "Duotone",
 
375
  "Lab Colour",
 
376
  "<invalid>"
 
377
};
 
378
 
 
379
 
 
380
static const gchar *prog_name = "PSD";
 
381
 
 
382
 
 
383
static void unpack_pb_channel(FILE *fd, guchar *dst, gint32 unpackedlen,
 
384
                                  guint32 *offset);
 
385
static void decode(long clen, long uclen, guchar *src, guchar *dst, int step);
 
386
static void packbitsdecode(long *clenp, long uclen,
 
387
                           guchar *src, guchar *dst, int step);
 
388
static void cmyk2rgb(guchar *src, guchar *destp,
 
389
                     long width, long height, int alpha);
 
390
static void cmykp2rgb(guchar *src, guchar *destp,
 
391
                      long width, long height, int alpha);
 
392
static void bitmap2gray(guchar *src,guchar *dest,long w,long h);
 
393
static guchar getguchar(FILE *fd, gchar *why);
 
394
static gshort getgshort(FILE *fd, gchar *why);
 
395
static glong getglong(FILE *fd, gchar *why);
 
396
static void xfread(FILE *fd, void *buf, long len, gchar *why);
 
397
static void xfread_interlaced(FILE *fd, guchar *buf, long len, gchar *why,
 
398
                              gint step);
 
399
static void read_whole_file(FILE *fd);
 
400
static void reshuffle_cmap(guchar *map256);
 
401
static gchar* getpascalstring(FILE *fd, gchar *why);
 
402
static gchar* getstring(size_t n, FILE * fd, gchar *why);
 
403
static void throwchunk(size_t n, FILE * fd, gchar *why);
 
404
static void dumpchunk(size_t n, FILE * fd, gchar *why);
 
405
static void seek_to_and_unpack_pixeldata(FILE* fd, gint layeri, gint channeli);
 
406
static void validate_aux_channel_name(gint aux_index);
 
407
 
 
408
 
 
409
MAIN ()
 
410
 
 
411
 
 
412
static void
 
413
query (void)
 
414
{
 
415
  static GimpParamDef load_args[] =
 
416
  {
 
417
    { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
 
418
    { GIMP_PDB_STRING, "filename", "The name of the file to load" },
 
419
    { GIMP_PDB_STRING, "raw_filename", "The name of the file to load" }
 
420
  };
 
421
  static GimpParamDef load_return_vals[] =
 
422
  {
 
423
    { GIMP_PDB_IMAGE, "image", "Output image" }
 
424
  };
 
425
 
 
426
  gimp_install_procedure ("file_psd_load",
 
427
                          "loads files of the Photoshop(tm) PSD file format",
 
428
                          "This filter loads files of Adobe Photoshop(tm) native PSD format.  These files may be of any image type supported by GIMP, with or without layers, layer masks, aux channels and guides.",
 
429
                          "Adam D. Moss & Torsten Martinsen",
 
430
                          "Adam D. Moss & Torsten Martinsen",
 
431
                          "1996-1998",
 
432
                          "Photoshop image",
 
433
                          NULL,
 
434
                          GIMP_PLUGIN,
 
435
                          G_N_ELEMENTS (load_args),
 
436
                          G_N_ELEMENTS (load_return_vals),
 
437
                          load_args, load_return_vals);
 
438
 
 
439
  gimp_register_file_handler_mime ("file_psd_load", "image/x-psd");
 
440
  gimp_register_magic_load_handler ("file_psd_load",
 
441
                                    "psd",
 
442
                                    "",
 
443
                                    "0,string,8BPS");
 
444
}
 
445
 
 
446
 
 
447
static void
 
448
run (const gchar      *name,
 
449
     gint              nparams,
 
450
     const GimpParam  *param,
 
451
     gint             *nreturn_vals,
 
452
     GimpParam       **return_vals)
 
453
{
 
454
  static GimpParam values[2];
 
455
  GimpRunMode run_mode;
 
456
  /*  GimpPDBStatusType status = GIMP_PDB_SUCCESS;*/
 
457
  gint32 image_ID;
 
458
 
 
459
  run_mode = param[0].data.d_int32;
 
460
 
 
461
  *nreturn_vals = 1;
 
462
  *return_vals = values;
 
463
  values[0].type = GIMP_PDB_STATUS;
 
464
  values[0].data.d_status = GIMP_PDB_CALLING_ERROR;
 
465
 
 
466
  if (strcmp (name, "file_psd_load") == 0)
 
467
    {
 
468
      image_ID = load_image (param[1].data.d_string);
 
469
 
 
470
      if (image_ID != -1)
 
471
        {
 
472
          *nreturn_vals = 2;
 
473
          values[0].data.d_status = GIMP_PDB_SUCCESS;
 
474
          values[1].type = GIMP_PDB_IMAGE;
 
475
          values[1].data.d_image = image_ID;
 
476
        }
 
477
      else
 
478
        {
 
479
          values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
 
480
        }
 
481
    }
 
482
}
 
483
 
 
484
 
 
485
static char *
 
486
sanitise_string(char *old_name)
 
487
{
 
488
  if (old_name) {
 
489
    char *rtn = gimp_any_to_utf8(old_name, -1,
 
490
                                 _("Invalid UTF-8 string in PSD file"));
 
491
    g_free(old_name);
 
492
    return rtn;
 
493
  } else {
 
494
    return NULL;
 
495
  }
 
496
}
 
497
 
 
498
static GimpImageType
 
499
psd_type_to_gimp_type (psd_imagetype psdtype)
 
500
{
 
501
  switch(psdtype)
 
502
    {
 
503
    case PSD_RGBA_IMAGE: return(GIMP_RGBA_IMAGE);
 
504
    case PSD_RGB_IMAGE: return(GIMP_RGB_IMAGE);
 
505
    case PSD_GRAYA_IMAGE: return(GIMP_GRAYA_IMAGE);
 
506
    case PSD_GRAY_IMAGE: return(GIMP_GRAY_IMAGE);
 
507
    case PSD_INDEXEDA_IMAGE: return(GIMP_INDEXEDA_IMAGE);
 
508
    case PSD_INDEXED_IMAGE: return(GIMP_INDEXED_IMAGE);
 
509
    case PSD_BITMAP_IMAGE: return(GIMP_GRAY_IMAGE);
 
510
    default: return(GIMP_RGB_IMAGE);
 
511
    }
 
512
}
 
513
 
 
514
static GimpLayerModeEffects
 
515
psd_lmode_to_gimp_lmode (gchar modekey[4])
 
516
{
 
517
  if (strncmp(modekey, "norm", 4)==0) return(GIMP_NORMAL_MODE);
 
518
  if (strncmp(modekey, "dark", 4)==0) return(GIMP_DARKEN_ONLY_MODE);
 
519
  if (strncmp(modekey, "lite", 4)==0) return(GIMP_LIGHTEN_ONLY_MODE);
 
520
  if (strncmp(modekey, "hue ", 4)==0) return(GIMP_HUE_MODE);
 
521
  if (strncmp(modekey, "sat ", 4)==0) return(GIMP_SATURATION_MODE);
 
522
  if (strncmp(modekey, "colr", 4)==0) return(GIMP_COLOR_MODE);
 
523
  if (strncmp(modekey, "mul ", 4)==0) return(GIMP_MULTIPLY_MODE);
 
524
  if (strncmp(modekey, "scrn", 4)==0) return(GIMP_SCREEN_MODE);
 
525
  if (strncmp(modekey, "diss", 4)==0) return(GIMP_DISSOLVE_MODE);
 
526
  if (strncmp(modekey, "diff", 4)==0) return(GIMP_DIFFERENCE_MODE);
 
527
  if (strncmp(modekey, "lum ", 4)==0) return(GIMP_VALUE_MODE);
 
528
  if (strncmp(modekey, "hLit", 4)==0) return(GIMP_HARDLIGHT_MODE);
 
529
  if (strncmp(modekey, "sLit", 4)==0) return(GIMP_SOFTLIGHT_MODE);
 
530
  if (strncmp(modekey, "over", 4)==0) return(GIMP_OVERLAY_MODE);
 
531
 
 
532
  printf("PSD: Warning - UNKNOWN layer-blend mode, reverting to 'normal'\n");
 
533
 
 
534
  return(GIMP_NORMAL_MODE);
 
535
}
 
536
 
 
537
static GimpImageBaseType
 
538
psd_type_to_gimp_base_type (psd_imagetype psdtype)
 
539
{
 
540
  switch(psdtype)
 
541
    {
 
542
    case PSD_RGBA_IMAGE:
 
543
    case PSD_RGB_IMAGE: return(GIMP_RGB);
 
544
    case PSD_BITMAP_IMAGE:
 
545
    case PSD_GRAYA_IMAGE:
 
546
    case PSD_GRAY_IMAGE: return(GIMP_GRAY);
 
547
    case PSD_INDEXEDA_IMAGE:
 
548
    case PSD_INDEXED_IMAGE: return(GIMP_INDEXED);
 
549
    default:
 
550
      g_message ("Error: Can't convert PSD imagetype to GIMP imagetype");
 
551
      gimp_quit();
 
552
      return(GIMP_RGB);
 
553
    }
 
554
}
 
555
 
 
556
static GimpUnit
 
557
psd_unit_to_gimp_unit (int psdunit)
 
558
{
 
559
  switch (psdunit)
 
560
    {
 
561
    case 1:
 
562
      return GIMP_UNIT_INCH;
 
563
    case 2: /* this means cm to PS, but MM is as close as we have */
 
564
      return GIMP_UNIT_MM;
 
565
    case 3:
 
566
      return GIMP_UNIT_POINT;
 
567
    case 4:
 
568
      return GIMP_UNIT_PICA;
 
569
    case 5: /* 5 == Columns, but what the heck is a column? */
 
570
    default:
 
571
      IFDBG printf("Warning: unable to convert psd unit %d to gimp unit\n", psdunit);
 
572
      return GIMP_UNIT_PIXEL;
 
573
    }
 
574
}
 
575
 
 
576
static GimpImageBaseType
 
577
psd_mode_to_gimp_base_type (gushort psdtype)
 
578
{
 
579
  switch(psdtype)
 
580
    {
 
581
    case 1: return GIMP_GRAY;
 
582
    case 2: return GIMP_INDEXED;
 
583
    case 3: return GIMP_RGB;
 
584
    default:
 
585
      g_message ("Error: Can't convert PSD mode to GIMP base imagetype");
 
586
      gimp_quit();
 
587
      return GIMP_RGB;
 
588
    }
 
589
}
 
590
 
 
591
static void
 
592
reshuffle_cmap (guchar *map256)
 
593
{
 
594
  guchar *tmpmap;
 
595
  int i;
 
596
 
 
597
  tmpmap = g_malloc(3 * 256);
 
598
 
 
599
  for (i = 0; i < 256; i++)
 
600
    {
 
601
      tmpmap[i*3  ] = map256[i];
 
602
      tmpmap[i*3+1] = map256[i+256];
 
603
      tmpmap[i*3+2] = map256[i+512];
 
604
    }
 
605
 
 
606
  memcpy(map256, tmpmap, 3 * 256);
 
607
  g_free(tmpmap);
 
608
}
 
609
 
 
610
static void
 
611
dispatch_resID(guint ID, FILE *fd, guint32 *offset, guint32 Size)
 
612
{
 
613
  if ( (ID < 0x0bb6) && (ID >0x07d0) )
 
614
    {
 
615
      IFDBG printf ("\t\tPath data is irrelevant to GIMP at this time.\n");
 
616
      throwchunk(Size, fd, "dispatch_res path throw");
 
617
      (*offset) += Size;
 
618
    }
 
619
  else
 
620
    switch (ID)
 
621
      {
 
622
      case 0x03ee:
 
623
        {
 
624
          gint32 remaining = Size;
 
625
 
 
626
          IFDBG printf ("\t\tALPHA CHANNEL NAMES:\n");
 
627
          if (Size > 0)
 
628
            {
 
629
              do
 
630
                {
 
631
                  guchar slen;
 
632
 
 
633
                  slen = getguchar (fd, "alpha channel name length");
 
634
                  (*offset)++;
 
635
                  remaining--;
 
636
 
 
637
                  /* Check for (Mac?) Photoshop (4?) file-writing bug */
 
638
                  if (slen > remaining)
 
639
                    {
 
640
                      IFDBG {printf("\nYay, a file bug.  "
 
641
                                    "Yuck.  Photoshop 4/Mac?  "
 
642
                                    "I'll work around you.\n");fflush(stdout);}
 
643
                      break;
 
644
                    }
 
645
 
 
646
                  if (slen)
 
647
                    {
 
648
                      guint32 alpha_name_len;
 
649
                      gchar* sname = getstring(slen, fd, "alpha channel name");
 
650
 
 
651
                      alpha_name_len = strlen(sname);
 
652
 
 
653
                      (*offset) += alpha_name_len;
 
654
                      remaining -= alpha_name_len;
 
655
 
 
656
                      sname = sanitise_string(sname);
 
657
 
 
658
                      psd_image.aux_channel[psd_image.num_aux_channels].name =
 
659
                        sname;
 
660
 
 
661
                      IFDBG printf("\t\t\tname: \"%s\"\n",
 
662
                                   psd_image.aux_channel[psd_image.num_aux_channels].name);
 
663
                    }
 
664
                  else
 
665
                    {
 
666
                      psd_image.aux_channel[psd_image.num_aux_channels].name =
 
667
                        NULL;
 
668
                      IFDBG
 
669
                        {printf("\t\t\tNull channel name %d.\n",
 
670
                                psd_image.num_aux_channels);fflush(stdout);}
 
671
                    }
 
672
 
 
673
                  psd_image.num_aux_channels++;
 
674
 
 
675
                  if (psd_image.num_aux_channels > MAX_CHANNELS)
 
676
                    {
 
677
                      printf("\nPSD: Sorry - this image has too many "
 
678
                             "aux channels.  Tell Adam!\n");
 
679
                      gimp_quit();
 
680
                    }
 
681
                }
 
682
              while (remaining > 0);
 
683
            }
 
684
 
 
685
          if (remaining)
 
686
            {
 
687
              IFDBG
 
688
                dumpchunk(remaining, fd, "alphaname padding 0 throw");
 
689
              else
 
690
                throwchunk(remaining, fd, "alphaname padding 0 throw");
 
691
              (*offset) += remaining;
 
692
              remaining = 0;
 
693
            }
 
694
        }
 
695
        break;
 
696
      case 0x03ef:
 
697
        IFDBG printf("\t\tDISPLAYINFO STRUCTURE: unhandled\n");
 
698
        throwchunk(Size, fd, "dispatch_res");
 
699
        (*offset) += Size;
 
700
        break;
 
701
      case 0x03f0: /* FIXME: untested */
 
702
        {
 
703
          psd_image.caption = getpascalstring(fd, "caption string");
 
704
          (*offset)++;
 
705
 
 
706
          if (psd_image.caption)
 
707
            {
 
708
              IFDBG printf("\t\t\tcontent: \"%s\"\n",psd_image.caption);
 
709
              (*offset) += strlen(psd_image.caption);
 
710
            }
 
711
        }
 
712
        break;
 
713
      case 0x03f2:
 
714
        IFDBG printf("\t\tBACKGROUND COLOR: unhandled\n");
 
715
        throwchunk(Size, fd, "dispatch_res");
 
716
        (*offset) += Size;
 
717
        break;
 
718
      case 0x03f4:
 
719
        IFDBG printf("\t\tGREY/MULTICHANNEL HALFTONING INFO: unhandled\n");
 
720
        throwchunk(Size, fd, "dispatch_res");
 
721
        (*offset) += Size;
 
722
        break;
 
723
      case 0x03f5:
 
724
        IFDBG printf("\t\tCOLOUR HALFTONING INFO: unhandled\n");
 
725
        throwchunk(Size, fd, "dispatch_res");
 
726
        (*offset) += Size;
 
727
        break;
 
728
      case 0x03f6:
 
729
        IFDBG printf("\t\tDUOTONE HALFTONING INFO: unhandled\n");
 
730
        throwchunk(Size, fd, "dispatch_res");
 
731
        (*offset) += Size;
 
732
        break;
 
733
      case 0x03f7:
 
734
        IFDBG printf("\t\tGREYSCALE/MULTICHANNEL TRANSFER FUNCTION: unhandled\n");
 
735
        throwchunk(Size, fd, "dispatch_res");
 
736
        (*offset) += Size;
 
737
        break;
 
738
      case 0x03f8:
 
739
        IFDBG printf("\t\tCOLOUR TRANSFER FUNCTION: unhandled\n");
 
740
        throwchunk(Size, fd, "dispatch_res");
 
741
        (*offset) += Size;
 
742
        break;
 
743
      case 0x03f9:
 
744
        IFDBG printf("\t\tDUOTONE TRANSFER FUNCTION: unhandled\n");
 
745
        throwchunk(Size, fd, "dispatch_res");
 
746
        (*offset) += Size;
 
747
        break;
 
748
      case 0x03fa:
 
749
        IFDBG printf("\t\tDUOTONE IMAGE INFO: unhandled\n");
 
750
        throwchunk(Size, fd, "dispatch_res");
 
751
        (*offset) += Size;
 
752
        break;
 
753
      case 0x03fb:
 
754
        IFDBG printf("\t\tEFFECTIVE BLACK/WHITE VALUES: unhandled\n");
 
755
        throwchunk(Size, fd, "dispatch_res");
 
756
        (*offset) += Size;
 
757
        break;
 
758
      case 0x03fe:
 
759
        IFDBG printf("\t\tQUICK MASK INFO: unhandled\n");
 
760
        throwchunk(Size, fd, "dispatch_res");
 
761
        (*offset) += Size;
 
762
        break;
 
763
      case 0x0400:
 
764
        {
 
765
          IFDBG printf("\t\tLAYER STATE INFO:\n");
 
766
          psd_image.active_layer_num = getgshort(fd, "ID target_layer_num");
 
767
 
 
768
          IFDBG printf("\t\t\ttarget: %d\n",(gint)psd_image.active_layer_num);
 
769
          (*offset) += 2;
 
770
        }
 
771
        break;
 
772
      case 0x0402:
 
773
        IFDBG printf("\t\tLAYER GROUP INFO: unhandled\n");
 
774
        IFDBG printf("\t\t\t(Inferred number of layers: %d)\n",(gint)(Size/2));
 
775
        throwchunk(Size, fd, "dispatch_res");
 
776
        (*offset) += Size;
 
777
        break;
 
778
      case 0x0405:
 
779
        IFDBG printf ("\t\tIMAGE MODE FOR RAW FORMAT: unhandled\n");
 
780
        throwchunk(Size, fd, "dispatch_res");
 
781
        (*offset) += Size;
 
782
        break;
 
783
      case 0x0408:
 
784
        {
 
785
          gint32 remaining = Size;
 
786
          int i;
 
787
          IFDBG printf ("\t\tGUIDE INFORMATION:\n");
 
788
 
 
789
          if (Size > 0)
 
790
            {
 
791
              gshort magic1, magic2, magic3, magic4, magic5, magic6;
 
792
              glong num_guides;
 
793
              PSDguide *guide;
 
794
 
 
795
              magic1 = getgshort(fd, "guide"); (*offset) += 2;
 
796
              magic2 = getgshort(fd, "guide"); (*offset) += 2;
 
797
              magic3 = getgshort(fd, "guide"); (*offset) += 2;
 
798
              magic4 = getgshort(fd, "guide"); (*offset) += 2;
 
799
              magic5 = getgshort(fd, "guide"); (*offset) += 2;
 
800
              magic6 = getgshort(fd, "guide"); (*offset) += 2;
 
801
              remaining -= 12;
 
802
 
 
803
              IFDBG printf("\t\t\tMagic: %d %d %d %d %d %d\n",
 
804
                           magic1, magic2, magic3, magic4, magic5, magic6);
 
805
              IFDBG printf("\t\t\tMagic: 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x\n",
 
806
                           magic1, magic2, magic3, magic4, magic5, magic6);
 
807
 
 
808
              num_guides = getglong(fd, "guide");
 
809
              (*offset) += 4; remaining -= 4;
 
810
 
 
811
              if (remaining != num_guides*5)
 
812
                {
 
813
                  IFDBG printf ("** FUNNY AMOUNT OF GUIDE DATA (%d)\n",
 
814
                                remaining);
 
815
                  goto funny_amount_of_guide_data;
 
816
                }
 
817
 
 
818
              IFDBG printf("\t\t\tNumber of guides is %ld\n", num_guides);
 
819
              psd_image.num_guides = num_guides;
 
820
              psd_image.guides = g_new(PSDguide, num_guides);
 
821
              guide = psd_image.guides;
 
822
 
 
823
              for (i = 0; i < num_guides; i++, guide++)
 
824
                {
 
825
                  guide->position = getglong(fd, "guide");
 
826
 
 
827
                  guide->horizontal = (1 == getguchar(fd, "guide"));
 
828
                  (*offset) += 5; remaining -= 5;
 
829
 
 
830
                  if (guide->horizontal)
 
831
                    {
 
832
                      guide->position =
 
833
                        RINT((double)(guide->position * (magic4>>8))
 
834
                             /(double)(magic4&255));
 
835
                    }
 
836
                  else
 
837
                    {
 
838
                      guide->position =
 
839
                        RINT((double)(guide->position * (magic6>>8))
 
840
                             /(double)(magic6&255));
 
841
                    }
 
842
 
 
843
                  IFDBG printf("\t\t\tGuide %d at %d, %s\n", i+1,
 
844
                               guide->position,
 
845
                               guide->horizontal ? "horizontal" : "vertical");
 
846
                }
 
847
            }
 
848
 
 
849
        funny_amount_of_guide_data:
 
850
 
 
851
          if (remaining)
 
852
            {
 
853
              IFDBG
 
854
                {
 
855
                  printf ("** GUIDE INFORMATION DROSS: ");
 
856
                  dumpchunk(remaining, fd, "dispatch_res");
 
857
                }
 
858
              else
 
859
                {
 
860
                  throwchunk(remaining, fd, "dispatch_res");
 
861
                }
 
862
 
 
863
              (*offset) += remaining;
 
864
            }
 
865
        }
 
866
        break;
 
867
      case 0x03ed:
 
868
        {
 
869
          IFDBG printf ("\t\tResolution Info:\n");
 
870
          psd_image.resolution_is_set = 1;
 
871
 
 
872
          psd_image.resolution.hRes = getglong(fd, "hRes");
 
873
          psd_image.resolution.hRes_unit = getgshort(fd, "hRes_unit");
 
874
          psd_image.resolution.widthUnit = getgshort(fd, "WidthUnit");
 
875
          psd_image.resolution.vRes = getglong(fd, "vRes");
 
876
          psd_image.resolution.vRes_unit = getgshort(fd, "vRes_unit");
 
877
          psd_image.resolution.heightUnit = getgshort(fd, "HeightUnit");
 
878
          (*offset) += Size;
 
879
          IFDBG  printf("\t\t\tres = %f, %f\n",
 
880
                        psd_image.resolution.hRes / 65536.0,
 
881
                        psd_image.resolution.vRes / 65536.0);
 
882
        } break;
 
883
 
 
884
      case 0x03e9:
 
885
      case 0x03f1:
 
886
      case 0x03f3:
 
887
      case 0x03fd:
 
888
      case 0x0401:
 
889
      case 0x0404:
 
890
      case 0x0406:
 
891
      case 0x0bb7:
 
892
      case 0x2710:
 
893
        IFDBG printf ("\t\t<Field is irrelevant to GIMP at this time.>\n");
 
894
        throwchunk(Size, fd, "dispatch_res");
 
895
        (*offset) += Size;
 
896
        break;
 
897
 
 
898
      case 0x03e8:
 
899
      case 0x03eb:
 
900
        IFDBG printf ("\t\t<Obsolete Photoshop 2.0 field.>\n");
 
901
        throwchunk(Size, fd, "dispatch_res");
 
902
        (*offset) += Size;
 
903
        break;
 
904
 
 
905
      case 0x03fc:
 
906
      case 0x03ff:
 
907
      case 0x0403:
 
908
        IFDBG printf ("\t\t<Obsolete field.>\n");
 
909
        throwchunk(Size, fd, "dispatch_res");
 
910
        (*offset) += Size;
 
911
        break;
 
912
 
 
913
      default:
 
914
        IFDBG
 
915
          {
 
916
            printf ("\t\t<Undocumented field.>\n");
 
917
            dumpchunk(Size, fd, "dispatch_res");
 
918
          }
 
919
        else
 
920
          throwchunk(Size, fd, "dispatch_res");
 
921
 
 
922
        (*offset) += Size;
 
923
        break;
 
924
      }
 
925
}
 
926
 
 
927
static void
 
928
do_layer_record(FILE *fd, guint32 *offset, gint layernum)
 
929
{
 
930
  PSDlayer *layer;
 
931
  gint32 top, left, bottom, right;
 
932
  guint32 extradatasize, layermaskdatasize, layerrangesdatasize;
 
933
  guint32 totaloff;
 
934
  gchar sig[4];
 
935
  guchar flags;
 
936
  gint i;
 
937
 
 
938
  IFDBG printf("\t\t\tLAYER RECORD (layer %d)\n", (int)layernum);
 
939
 
 
940
  layer = psd_image.layer + layernum;
 
941
 
 
942
  /* table 11-12 */
 
943
  top = getglong (fd, "layer top");
 
944
  (*offset)+=4;
 
945
  left = getglong (fd, "layer left");
 
946
  (*offset)+=4;
 
947
  bottom = getglong (fd, "layer bottom");
 
948
  (*offset)+=4;
 
949
  right = getglong (fd, "layer right");
 
950
  (*offset)+=4;
 
951
 
 
952
  layer->x = left;
 
953
  layer->y = top;
 
954
  layer->width = right - left;
 
955
  layer->height = bottom - top;
 
956
 
 
957
  IFDBG printf("\t\t\t\tLayer extents: (%d,%d) -> (%d,%d)\n",
 
958
               left,top,right,bottom);
 
959
 
 
960
  layer->num_channels = getgshort (fd, "layer num_channels");
 
961
  (*offset)+=2;
 
962
 
 
963
  IFDBG printf("\t\t\t\tNumber of channels: %d\n",
 
964
               (int)layer->num_channels);
 
965
 
 
966
  if (layer->num_channels)
 
967
    {
 
968
      layer->channel = g_new(PSDchannel, layer->num_channels);
 
969
 
 
970
      for (i = 0; i < layer->num_channels; i++)
 
971
        {
 
972
          PSDchannel *channel = layer->channel + i;
 
973
 
 
974
          /* table 11-13 */
 
975
          IFDBG printf("\t\t\t\tCHANNEL LENGTH INFO (%d)\n", i);
 
976
 
 
977
          channel->type = getgshort(fd, "channel id");
 
978
          (*offset)+=2;
 
979
          IFDBG printf("\t\t\t\t\tChannel TYPE: %d\n", channel->type);
 
980
 
 
981
          channel->compressedsize = getglong(fd, "channeldatalength");
 
982
          (*offset)+=4;
 
983
          IFDBG printf("\t\t\t\t\tChannel Data Length: %d\n",
 
984
                       channel->compressedsize);
 
985
        }
 
986
    } else {
 
987
      IFDBG printf("\t\t\t\tOo-er, layer has no channels.  Hmm.\n");
 
988
    }
 
989
 
 
990
  xfread(fd, sig, 4, "layer blend sig");
 
991
  (*offset)+=4;
 
992
 
 
993
  if (strncmp(sig, "8BIM", 4)!=0)
 
994
    {
 
995
      IFDBG printf("\t\t\t(layer blend signature '%c%c%c%c' is incorrect: quitting)\n", sig[0], sig[1], sig[2], sig[3]);
 
996
      g_message ("Error: layer blend signature is incorrect. :-(");
 
997
      gimp_quit();
 
998
    }
 
999
 
 
1000
  xfread(fd, layer->blendkey, 4, "layer blend key");
 
1001
  (*offset)+=4;
 
1002
 
 
1003
  IFDBG printf("\t\t\t\tBlend type: PSD(\"%c%c%c%c\") = GIMP(%d)\n",
 
1004
               layer->blendkey[0],
 
1005
               layer->blendkey[1],
 
1006
               layer->blendkey[2],
 
1007
               layer->blendkey[3],
 
1008
               psd_lmode_to_gimp_lmode(layer->blendkey));
 
1009
 
 
1010
  layer->opacity = getguchar(fd, "layer opacity");
 
1011
  (*offset)++;
 
1012
 
 
1013
  IFDBG printf("\t\t\t\tLayer Opacity: %d\n", layer->opacity);
 
1014
 
 
1015
  layer->clipping = getguchar(fd, "layer clipping");
 
1016
  (*offset)++;
 
1017
 
 
1018
  IFDBG printf("\t\t\t\tLayer Clipping: %d (%s)\n",
 
1019
               layer->clipping,
 
1020
               layer->clipping == 0 ? "base" : "non-base");
 
1021
 
 
1022
  flags = getguchar(fd, "layer flags");
 
1023
  (*offset)++;
 
1024
 
 
1025
  IFDBG printf("\t\t\t\tLayer Flags: %d (%s, %s)\n", flags,
 
1026
         flags&1?"preserve transparency":"don't preserve transparency",
 
1027
         flags&2?"visible":"not visible");
 
1028
 
 
1029
  layer->protecttrans = (flags & 1) ? TRUE : FALSE;
 
1030
  layer->visible = (flags & 2) ? FALSE : TRUE;
 
1031
 
 
1032
  getguchar(fd, "layer record filler");
 
1033
  (*offset)++;
 
1034
 
 
1035
  extradatasize = getglong(fd, "layer extra data size");
 
1036
  (*offset)+=4;
 
1037
  IFDBG printf("\t\t\t\tEXTRA DATA SIZE: %d\n",extradatasize);
 
1038
 
 
1039
  /* FIXME: should do something with this data */
 
1040
  /*throwchunk(extradatasize, fd, "layer extradata throw");
 
1041
  (*offset) += extradatasize;*/
 
1042
 
 
1043
  totaloff = (*offset) + extradatasize;
 
1044
 
 
1045
  /* table 11-14 */
 
1046
  layermaskdatasize = getglong(fd, "layer mask data size");
 
1047
  (*offset)+=4;
 
1048
  IFDBG printf("\t\t\t\t\tLAYER MASK DATA SIZE: %d\n", layermaskdatasize);
 
1049
 
 
1050
  if (layermaskdatasize)
 
1051
    {
 
1052
      top    = getglong(fd, "lmask top");
 
1053
      (*offset) += 4;
 
1054
      left   = getglong(fd, "lmask left");
 
1055
      (*offset) += 4;
 
1056
      bottom = getglong(fd, "lmask bottom");
 
1057
      (*offset) += 4;
 
1058
      right  = getglong(fd, "lmask right");
 
1059
      (*offset) += 4;
 
1060
 
 
1061
      layer->lm_x = left;
 
1062
      layer->lm_y = top;
 
1063
      layer->lm_width = right - left;
 
1064
      layer->lm_height = bottom - top;
 
1065
 
 
1066
      getglong(fd, "lmask data throw");
 
1067
      (*offset) += 4;
 
1068
 
 
1069
      /*      throwchunk(layermaskdatasize, fd, "layer mask data throw");
 
1070
      (*offset) += layermaskdatasize;*/
 
1071
    }
 
1072
 
 
1073
  layerrangesdatasize = getglong(fd, "layer ranges data size");
 
1074
  (*offset)+=4;
 
1075
  IFDBG printf("\t\t\t\t\t\tLAYER RANGES DATA SIZE: %d\n",layermaskdatasize);
 
1076
 
 
1077
  if (layerrangesdatasize)
 
1078
    {
 
1079
      throwchunk(layerrangesdatasize, fd, "layer ranges data throw");
 
1080
      (*offset) += layerrangesdatasize;
 
1081
    }
 
1082
 
 
1083
  layer->name = getpascalstring(fd, "layer name");
 
1084
  (*offset)++;
 
1085
 
 
1086
  if (layer->name)
 
1087
    {
 
1088
      (*offset) += strlen(layer->name);
 
1089
      IFDBG printf("\t\t\t\t\t\tLAYER NAME: '%s'\n", layer->name);
 
1090
      layer->name = sanitise_string(layer->name);
 
1091
    }
 
1092
  else
 
1093
    {
 
1094
      IFDBG printf("\t\t\t\t\t\tNULL LAYER NAME\n");
 
1095
    }
 
1096
  /* If no layermask data - set offset and size from layer data */
 
1097
  if (! layermaskdatasize)
 
1098
    {
 
1099
      IFDBG
 
1100
        fprintf(stderr, "Setting layer mask data layer\n");
 
1101
 
 
1102
      psd_image.layer[layernum].lm_x = psd_image.layer[layernum].x;
 
1103
      psd_image.layer[layernum].lm_y = psd_image.layer[layernum].y;
 
1104
      psd_image.layer[layernum].lm_width =  psd_image.layer[layernum].width;
 
1105
      psd_image.layer[layernum].lm_height = psd_image.layer[layernum].height;
 
1106
    }
 
1107
 
 
1108
  if (totaloff-(*offset) > 0)
 
1109
    {
 
1110
      IFDBG
 
1111
        {
 
1112
          printf("Warning: layer record dross: ");
 
1113
          dumpchunk(totaloff-(*offset), fd, "layer record dross throw");
 
1114
        }
 
1115
      else
 
1116
        {
 
1117
          throwchunk(totaloff-(*offset), fd, "layer record dross throw");
 
1118
        }
 
1119
      (*offset) = totaloff;
 
1120
    }
 
1121
}
 
1122
 
 
1123
static void
 
1124
do_layer_struct(FILE *fd, guint32 *offset)
 
1125
{
 
1126
  gint i;
 
1127
 
 
1128
  IFDBG printf("\t\tLAYER STRUCTURE SECTION\n");
 
1129
 
 
1130
  psd_image.num_layers = getgshort(fd, "layer struct numlayers");
 
1131
  (*offset)+=2;
 
1132
 
 
1133
  IFDBG printf("\t\t\tCanonical number of layers: %d%s\n",
 
1134
         psd_image.num_layers>0?
 
1135
         (int)psd_image.num_layers:abs(psd_image.num_layers),
 
1136
         psd_image.num_layers>0?"":" (absolute/alpha)");
 
1137
 
 
1138
  if (psd_image.num_layers < 0)
 
1139
    {
 
1140
      psd_image.num_layers = -psd_image.num_layers;
 
1141
      psd_image.absolute_alpha = TRUE;
 
1142
    }
 
1143
  else
 
1144
    {
 
1145
      psd_image.absolute_alpha = FALSE;
 
1146
    }
 
1147
 
 
1148
  psd_image.layer = g_new(PSDlayer, psd_image.num_layers);
 
1149
 
 
1150
  for (i = 0; i < psd_image.num_layers; i++)
 
1151
    {
 
1152
      do_layer_record(fd, offset, i);
 
1153
    }
 
1154
}
 
1155
 
 
1156
static void
 
1157
do_layer_pixeldata(FILE *fd, guint32 *offset)
 
1158
{
 
1159
  gint layeri, channeli;
 
1160
 
 
1161
  for (layeri = 0; layeri < psd_image.num_layers; layeri++)
 
1162
    {
 
1163
      PSDlayer *layer = psd_image.layer + layeri;
 
1164
 
 
1165
      for (channeli = 0; channeli < layer->num_channels; channeli++)
 
1166
        {
 
1167
          PSDchannel *channel = layer->channel + channeli;
 
1168
 
 
1169
          if (channel->type == -2)
 
1170
            {
 
1171
              channel->width = layer->lm_width;
 
1172
              channel->height = layer->lm_height;
 
1173
            }
 
1174
          else
 
1175
            {
 
1176
              channel->width = layer->width;
 
1177
              channel->height = layer->height;
 
1178
            }
 
1179
 
 
1180
          fgetpos(fd, &channel->fpos);
 
1181
 
 
1182
          throwchunk(channel->compressedsize, fd, "channel data skip");
 
1183
          (*offset) += channel->compressedsize;
 
1184
        }
 
1185
    }
 
1186
}
 
1187
 
 
1188
static void
 
1189
seek_to_and_unpack_pixeldata(FILE* fd, gint layeri, gint channeli)
 
1190
{
 
1191
  int width, height;
 
1192
  guchar *tmpline;
 
1193
  gint compression;
 
1194
  guint32 offset = 0;
 
1195
  PSDchannel *channel = &psd_image.layer[layeri].channel[channeli];
 
1196
 
 
1197
  fsetpos(fd, &channel->fpos);
 
1198
 
 
1199
  compression = getgshort(fd, "layer channel compression type");
 
1200
  offset+=2;
 
1201
 
 
1202
  width = channel->width;
 
1203
  height = channel->height;
 
1204
 
 
1205
  IFDBG
 
1206
    {
 
1207
      printf("\t\t\tLayer (%d) Channel (%d:%d) Compression: %d (%s)\n",
 
1208
             layeri,
 
1209
             channeli,
 
1210
             channel->type,
 
1211
             compression,
 
1212
             compression==0?"raw":(compression==1?"RLE":"*UNKNOWN!*"));
 
1213
 
 
1214
      fflush(stdout);
 
1215
    }
 
1216
 
 
1217
  channel->data = g_malloc (width * height);
 
1218
 
 
1219
  tmpline = g_malloc(width + 1);
 
1220
 
 
1221
  switch (compression)
 
1222
    {
 
1223
    case 0: /* raw data */
 
1224
      {
 
1225
        gint linei;
 
1226
 
 
1227
        for (linei = 0; linei < height; linei++)
 
1228
          {
 
1229
            xfread(fd, channel->data + linei * width, width,
 
1230
                   "raw channel line");
 
1231
            offset += width;
 
1232
          }
 
1233
 
 
1234
#if 0
 
1235
        /* Pad raw data to multiple of 2? */
 
1236
        if ((height * width) & 1)
 
1237
          {
 
1238
            getguchar(fd, "raw channel padding");
 
1239
            offset++;
 
1240
          }
 
1241
#endif
 
1242
      }
 
1243
      break;
 
1244
    case 1: /* RLE, one row at a time, padded to an even width */
 
1245
      {
 
1246
        gint linei;
 
1247
        gint blockread;
 
1248
 
 
1249
        /* we throw this away because in theory we can trust the
 
1250
           data to unpack to the right length... hmm... */
 
1251
        throwchunk(height * 2, fd, "widthlist");
 
1252
        offset += height * 2;
 
1253
 
 
1254
        blockread = offset;
 
1255
 
 
1256
        /*IFDBG {printf("\nHere comes the guitar solo...\n");
 
1257
          fflush(stdout);}*/
 
1258
 
 
1259
        for (linei=0; linei<height; linei++)
 
1260
          {
 
1261
            /*printf(" %d ", *offset);*/
 
1262
            unpack_pb_channel(fd, tmpline,
 
1263
                              width
 
1264
                              /*+ (width&1)*/,
 
1265
                              &offset);
 
1266
            memcpy(channel->data + linei * width, tmpline, width);
 
1267
          }
 
1268
 
 
1269
        IFDBG {printf("\t\t\t\t\tActual compressed size was %d bytes\n"
 
1270
                      , offset-blockread);fflush(stdout);}
 
1271
      }
 
1272
      break;
 
1273
    default: /* *unknown* */
 
1274
      IFDBG {printf("\nEEP!\n");fflush(stdout);}
 
1275
      g_message ("*** Unknown compression type in channel.");
 
1276
      gimp_quit();
 
1277
      break;
 
1278
    }
 
1279
  g_free(tmpline);
 
1280
}
 
1281
 
 
1282
static void
 
1283
do_layers(FILE *fd, guint32 *offset)
 
1284
{
 
1285
  guint32 section_length;
 
1286
 
 
1287
  section_length = getglong(fd, "layerinfo sectionlength");
 
1288
  (*offset)+=4;
 
1289
 
 
1290
  IFDBG printf("\tLAYER INFO SECTION\n");
 
1291
  IFDBG printf("\t\tSECTION LENGTH: %u\n",section_length);
 
1292
 
 
1293
  do_layer_struct(fd, offset);
 
1294
 
 
1295
  do_layer_pixeldata(fd, offset);
 
1296
}
 
1297
 
 
1298
static void
 
1299
do_layer_and_mask(FILE *fd)
 
1300
{
 
1301
  guint32 offset = 0;
 
1302
  guint32 Size = PSDheader.miscsizelen;
 
1303
 
 
1304
  guint32 offset_now = ftell(fd);
 
1305
 
 
1306
  IFDBG printf("LAYER AND MASK INFO\n");
 
1307
  IFDBG printf("\tSECTION LENGTH: %u\n",Size);
 
1308
 
 
1309
  if (Size == 0) return;
 
1310
 
 
1311
  do_layers(fd, &offset);
 
1312
 
 
1313
  IFDBG {printf("And...?\n");fflush(stdout);}
 
1314
 
 
1315
  if (offset < Size)
 
1316
    {
 
1317
      IFDBG
 
1318
        {
 
1319
          printf("PSD: Supposedly there are %d bytes of mask info left.\n",
 
1320
                 Size-offset);
 
1321
          if ((Size-offset == 4) || (Size-offset == 24))
 
1322
            printf("     That sounds good to me.\n");
 
1323
          else
 
1324
            printf("     That sounds strange to me.\n");
 
1325
        }
 
1326
 
 
1327
 
 
1328
      /*      if ((getguchar(fd, "mask info throw")!=0) ||
 
1329
          (getguchar(fd, "mask info throw")!=0) ||
 
1330
          (getguchar(fd, "mask info throw")!=0) ||
 
1331
          (getguchar(fd, "mask info throw")!=0))
 
1332
        {
 
1333
          printf("*** This mask info block looks pretty bogus.\n");
 
1334
        }*/
 
1335
    }
 
1336
  else
 
1337
    printf("PSD: Stern warning - no mask info.\n");
 
1338
 
 
1339
 
 
1340
  /* If 'offset' wasn't being buggily updated, we wouldn't need this. (!?) */
 
1341
  fseek(fd, Size+offset_now, SEEK_SET);
 
1342
}
 
1343
 
 
1344
static void
 
1345
do_image_resources(FILE *fd)
 
1346
{
 
1347
  guint16 ID;
 
1348
  gchar *Name;
 
1349
  guint32 Size;
 
1350
 
 
1351
  guint32 offset = 0;
 
1352
 
 
1353
  IFDBG printf("IMAGE RESOURCE BLOCK:\n");
 
1354
 
 
1355
  psd_image.resolution_is_set = 0;
 
1356
 
 
1357
  /* FIXME: too trusting that the file isn't corrupt */
 
1358
  while (offset < PSDheader.imgreslen-1)
 
1359
    {
 
1360
      gchar sig[4];
 
1361
 
 
1362
      xfread(fd, sig, 4, "imageresources signature");
 
1363
      offset += 4;
 
1364
 
 
1365
 
 
1366
      /* generic information about a block ID */
 
1367
 
 
1368
 
 
1369
      ID = getgshort(fd, "ID num");
 
1370
      offset += 2;
 
1371
      IFDBG printf("\tID: 0x%04x / ",ID);
 
1372
 
 
1373
      Name = getpascalstring(fd, "ID name");
 
1374
      offset++;
 
1375
 
 
1376
      if (Name)
 
1377
        {
 
1378
          IFDBG printf("\"%s\" ", Name);
 
1379
          offset += strlen(Name);
 
1380
 
 
1381
          if (!(strlen(Name)&1))
 
1382
            {
 
1383
              throwchunk(1, fd, "ID name throw");
 
1384
              offset ++;
 
1385
            }
 
1386
          g_free(Name);
 
1387
        }
 
1388
      else
 
1389
        {
 
1390
          throwchunk(1, fd, "ID name throw2");
 
1391
          offset++;
 
1392
        }
 
1393
 
 
1394
      Size = getglong(fd, "ID Size");
 
1395
      offset += 4;
 
1396
      IFDBG printf("Size: %d\n", Size);
 
1397
 
 
1398
      if (strncmp(sig, "8BIM", 4) == 0)
 
1399
        dispatch_resID(ID, fd, &offset, Size);
 
1400
      else
 
1401
        {
 
1402
          printf("PSD: Warning, unknown resource signature \"%.4s\" at or before offset %d ::: skipping\n", sig, offset - 8);
 
1403
          throwchunk(Size, fd, "Skipping Unknown Resource");
 
1404
          offset += Size;
 
1405
        }
 
1406
 
 
1407
      if (Size&1)
 
1408
        {
 
1409
          IFDBG printf("+1");
 
1410
          throwchunk(1, fd, "ID content throw");
 
1411
          offset ++;
 
1412
        }
 
1413
    }
 
1414
 
 
1415
  /*  if (offset != PSDheader.imgreslen)
 
1416
    {
 
1417
      printf("\tSucking imageres byte...\n");
 
1418
      throwchunk(1, fd, "imageres suck");
 
1419
      offset ++;
 
1420
    }*/
 
1421
 
 
1422
}
 
1423
 
 
1424
 
 
1425
#if PANOTOOLS_FIX
 
1426
/* Convert RGB data to RGBA data */
 
1427
static guchar*
 
1428
RGB_to_RGBA (const guchar* rgb_data, gint numpix)
 
1429
{
 
1430
  guchar* rtn;
 
1431
  int i,j;
 
1432
 
 
1433
  if (!rgb_data)
 
1434
    {
 
1435
      printf("NULL rgb data - eep!");
 
1436
      return NULL;
 
1437
    }
 
1438
 
 
1439
  rtn = g_malloc(numpix * 4);
 
1440
 
 
1441
  j = 0;
 
1442
  for (i=0; i<numpix; i++)
 
1443
    {
 
1444
      rtn[j++] = rgb_data[i*3];
 
1445
      rtn[j++] = rgb_data[i*3+1];
 
1446
      rtn[j++] = rgb_data[i*3+2];
 
1447
      rtn[j++] = 255;
 
1448
    }
 
1449
 
 
1450
  return rtn;
 
1451
}
 
1452
#endif /* PANOTOOLS_FIX */
 
1453
 
 
1454
 
 
1455
static guchar*
 
1456
chans_to_GRAYA (const guchar* grey,
 
1457
                const guchar* alpha,
 
1458
                gint numpix)
 
1459
{
 
1460
  guchar* rtn;
 
1461
  int i;
 
1462
 
 
1463
  if (!grey || !alpha)
 
1464
    {
 
1465
      printf("NULL channel - eep!");
 
1466
      return NULL;
 
1467
    }
 
1468
 
 
1469
  rtn = g_malloc(numpix * 2);
 
1470
 
 
1471
  for (i = 0; i < numpix; i++)
 
1472
    {
 
1473
      rtn[i*2  ] = grey[i];
 
1474
      rtn[i*2+1] = alpha[i];
 
1475
    }
 
1476
 
 
1477
  return rtn;
 
1478
}
 
1479
 
 
1480
static guchar*
 
1481
chans_to_RGB (const guchar* red,
 
1482
              const guchar* green,
 
1483
              const guchar* blue,
 
1484
              gint numpix)
 
1485
{
 
1486
  guchar* rtn;
 
1487
  int i;
 
1488
 
 
1489
  if (!red || !green || !blue)
 
1490
    {
 
1491
      printf("NULL channel - eep!");
 
1492
      return NULL;
 
1493
    }
 
1494
 
 
1495
  rtn = g_malloc(numpix * 3);
 
1496
 
 
1497
  for (i=0; i<numpix; i++)
 
1498
    {
 
1499
      rtn[i*3  ] = red[i];
 
1500
      rtn[i*3+1] = green[i];
 
1501
      rtn[i*3+2] = blue[i];
 
1502
    }
 
1503
 
 
1504
  return rtn;
 
1505
}
 
1506
 
 
1507
static guchar*
 
1508
chans_to_RGBA (const guchar* red,
 
1509
               const guchar* green,
 
1510
               const guchar* blue,
 
1511
               const guchar* alpha,
 
1512
               gint numpix)
 
1513
{
 
1514
  guchar* rtn;
 
1515
  int i;
 
1516
  gboolean careful = FALSE;
 
1517
 
 
1518
  if (!red || !green || !blue || !alpha)
 
1519
    {
 
1520
      printf("chans_to_RGBA : NULL channel - eep!");
 
1521
      careful = TRUE;
 
1522
    }
 
1523
 
 
1524
  rtn = g_malloc(numpix * 4);
 
1525
 
 
1526
  if (!careful)
 
1527
    {
 
1528
      for (i=0; i<numpix; i++)
 
1529
        {
 
1530
          rtn[i*4  ] = red[i];
 
1531
          rtn[i*4+1] = green[i];
 
1532
          rtn[i*4+2] = blue[i];
 
1533
          rtn[i*4+3] = alpha[i];
 
1534
        }
 
1535
    }
 
1536
  else
 
1537
    {
 
1538
      for (i=0; i<numpix; i++)
 
1539
        {
 
1540
          rtn[i*4  ] = (red==NULL) ? 0 : red[i];
 
1541
          rtn[i*4+1] = (green==NULL) ? 0 : green[i];
 
1542
          rtn[i*4+2] = (blue==NULL) ? 0 : blue[i];
 
1543
          rtn[i*4+3] = (alpha==NULL) ? 0 : alpha[i];
 
1544
        }
 
1545
    }
 
1546
 
 
1547
  return rtn;
 
1548
}
 
1549
 
 
1550
static
 
1551
gboolean psd_layer_has_alpha(PSDlayer* layer)
 
1552
{
 
1553
  int i;
 
1554
 
 
1555
  for (i = 0; i < layer->num_channels; i++)
 
1556
    {
 
1557
      if (layer->channel[i].type == -1)
 
1558
        {
 
1559
          return TRUE;
 
1560
        }
 
1561
    }
 
1562
  return FALSE;
 
1563
}
 
1564
 
 
1565
static
 
1566
void validate_aux_channel_name(gint aux_index)
 
1567
{
 
1568
  if (psd_image.aux_channel[aux_index].name == NULL)
 
1569
    {
 
1570
      if (aux_index == 0)
 
1571
        psd_image.aux_channel[aux_index].name = g_strdup ("Aux channel");
 
1572
      else
 
1573
        psd_image.aux_channel[aux_index].name =
 
1574
          g_strdup_printf ("Aux channel #%d", aux_index);
 
1575
    }
 
1576
}
 
1577
 
 
1578
static
 
1579
void extract_data_and_channels(guchar* src, gint gimpstep, gint psstep,
 
1580
                               gint32 image_ID, GimpDrawable* drawable,
 
1581
                               gint width, gint height)
 
1582
{
 
1583
  guchar* primary_data;
 
1584
  guchar* aux_data;
 
1585
  GimpPixelRgn pixel_rgn;
 
1586
 
 
1587
  IFDBG printf("Extracting primary channel data (%d channels)\n"
 
1588
         "\tand %d auxiliary channels.\n", gimpstep, psstep-gimpstep);
 
1589
 
 
1590
  /* gimp doesn't like 0 width/height drawables. */
 
1591
  if ((width == 0) || (height == 0))
 
1592
    {
 
1593
      IFDBG printf("(bad channel dimensions -- skipping)");
 
1594
      return;
 
1595
    }
 
1596
 
 
1597
  primary_data = g_malloc(width * height * gimpstep);
 
1598
  {
 
1599
    int pix,chan;
 
1600
 
 
1601
    for (pix = 0; pix < width * height; pix++)
 
1602
      {
 
1603
        for (chan = 0; chan < gimpstep; chan++)
 
1604
          {
 
1605
            primary_data[pix * gimpstep + chan] = src[pix * psstep + chan];
 
1606
          }
 
1607
      }
 
1608
    gimp_pixel_rgn_init (&pixel_rgn, drawable,
 
1609
                         0, 0, drawable->width, drawable->height,
 
1610
                         TRUE, FALSE);
 
1611
    gimp_pixel_rgn_set_rect (&pixel_rgn, primary_data,
 
1612
                             0, 0, drawable->width, drawable->height);
 
1613
 
 
1614
    gimp_drawable_flush (drawable);
 
1615
    gimp_drawable_detach (drawable);
 
1616
  }
 
1617
  g_free (primary_data);
 
1618
 
 
1619
  aux_data = g_malloc (width * height);
 
1620
  {
 
1621
    int pix, chan, aux_index;
 
1622
    gint32 channel_ID;
 
1623
    GimpDrawable* chdrawable;
 
1624
    GimpRGB colour;
 
1625
 
 
1626
    gimp_rgb_set (&colour, 0.0, 0.0, 0.0);
 
1627
 
 
1628
    for (chan=gimpstep; chan<psstep; chan++)
 
1629
      {
 
1630
        for (pix = 0; pix < width * height; pix++)
 
1631
          {
 
1632
            aux_data [pix] = src [pix * psstep + chan];
 
1633
          }
 
1634
 
 
1635
        aux_index = chan - gimpstep;
 
1636
        validate_aux_channel_name (aux_index);
 
1637
 
 
1638
        channel_ID = gimp_channel_new (image_ID,
 
1639
                                       psd_image.aux_channel[aux_index].name,
 
1640
                                       width, height,
 
1641
                                       100.0, &colour);
 
1642
        gimp_image_add_channel (image_ID, channel_ID, 0);
 
1643
        gimp_drawable_set_visible (channel_ID, FALSE);
 
1644
 
 
1645
        chdrawable = gimp_drawable_get (channel_ID);
 
1646
 
 
1647
        gimp_pixel_rgn_init (&pixel_rgn, chdrawable,
 
1648
                             0, 0, chdrawable->width, chdrawable->height,
 
1649
                             TRUE, FALSE);
 
1650
        gimp_pixel_rgn_set_rect (&pixel_rgn, aux_data,
 
1651
                                 0, 0, chdrawable->width, chdrawable->height);
 
1652
 
 
1653
        gimp_drawable_flush (chdrawable);
 
1654
        gimp_drawable_detach (chdrawable);
 
1655
      }
 
1656
  }
 
1657
  g_free(aux_data);
 
1658
 
 
1659
  IFDBG printf("Done with that.\n\n");
 
1660
}
 
1661
 
 
1662
static void
 
1663
extract_channels(guchar* src, gint num_wanted, gint psstep,
 
1664
                 gint32 image_ID,
 
1665
                 gint width, gint height)
 
1666
{
 
1667
  guchar* aux_data;
 
1668
  GimpPixelRgn pixel_rgn;
 
1669
 
 
1670
  IFDBG printf("Extracting %d/%d auxiliary channels.\n", num_wanted, psstep);
 
1671
 
 
1672
  /* gimp doesn't like 0 width/height drawables. */
 
1673
  if ((width == 0) || (height == 0))
 
1674
    {
 
1675
      IFDBG printf("(bad channel dimensions -- skipping)");
 
1676
      return;
 
1677
    }
 
1678
 
 
1679
  aux_data = g_malloc(width * height);
 
1680
  {
 
1681
    int pix, chan, aux_index;
 
1682
    gint32 channel_ID;
 
1683
    GimpDrawable* chdrawable;
 
1684
    GimpRGB colour;
 
1685
 
 
1686
    gimp_rgb_set (&colour, 0.0, 0.0, 0.0);
 
1687
 
 
1688
    for (chan=psstep-num_wanted; chan<psstep; chan++)
 
1689
      {
 
1690
        for (pix = 0; pix < width * height; pix++)
 
1691
          {
 
1692
            aux_data [pix] = src [pix * psstep + chan];
 
1693
          }
 
1694
 
 
1695
        aux_index = chan - (psstep - num_wanted);
 
1696
        validate_aux_channel_name (aux_index);
 
1697
 
 
1698
        channel_ID = gimp_channel_new (image_ID,
 
1699
                                       psd_image.aux_channel[aux_index].name,
 
1700
                                       width, height,
 
1701
                                       100.0, &colour);
 
1702
        gimp_image_add_channel (image_ID, channel_ID, 0);
 
1703
        gimp_drawable_set_visible (channel_ID, FALSE);
 
1704
 
 
1705
        chdrawable = gimp_drawable_get (channel_ID);
 
1706
 
 
1707
        gimp_pixel_rgn_init (&pixel_rgn, chdrawable,
 
1708
                             0, 0, chdrawable->width, chdrawable->height,
 
1709
                             TRUE, FALSE);
 
1710
        gimp_pixel_rgn_set_rect (&pixel_rgn, aux_data,
 
1711
                                 0, 0, chdrawable->width, chdrawable->height);
 
1712
 
 
1713
        gimp_drawable_flush (chdrawable);
 
1714
        gimp_drawable_detach (chdrawable);
 
1715
      }
 
1716
  }
 
1717
  g_free(aux_data);
 
1718
 
 
1719
  IFDBG printf("Done with that.\n\n");
 
1720
}
 
1721
 
 
1722
static void
 
1723
resize_mask(guchar* src, guchar* dest,
 
1724
            gint32 src_x, gint32 src_y,
 
1725
            gint32 src_w, gint32 src_h,
 
1726
            gint32 dest_w, gint32 dest_h)
 
1727
{
 
1728
  int x,y;
 
1729
 
 
1730
  IFDBG printf("--> %p %p : %d %d . %d %d . %d %d\n",
 
1731
         src, dest,
 
1732
         src_x, src_y,
 
1733
         src_w, src_h,
 
1734
         dest_w, dest_h);
 
1735
 
 
1736
  for (y=0; y<dest_h; y++)
 
1737
    {
 
1738
      for (x=0; x<dest_w; x++)
 
1739
        {
 
1740
          /* Avoid a 1-pixel border top-left */
 
1741
          if ((x>=src_x) && (x<src_x+src_w) &&
 
1742
              (y>=src_y) && (y<src_y+src_h))
 
1743
            {
 
1744
              dest[dest_w * y + x] =
 
1745
                src[src_w * (y-src_y) + (x-src_x)];
 
1746
            }
 
1747
          else
 
1748
            {
 
1749
              dest[dest_w * y + x] = 255;
 
1750
            }
 
1751
        }
 
1752
    }
 
1753
}
 
1754
 
 
1755
static gint32
 
1756
load_image (const gchar *name)
 
1757
{
 
1758
  FILE *fd;
 
1759
  gboolean want_aux;
 
1760
  char *name_buf;
 
1761
  guchar *cmykbuf;
 
1762
  guchar *dest = NULL, *temp;
 
1763
  long channels, nguchars;
 
1764
  psd_imagetype imagetype;
 
1765
  gboolean cmyk = FALSE;
 
1766
  gint step = 1;
 
1767
  gint32 image_ID = -1;
 
1768
  gint32 layer_ID = -1;
 
1769
  GimpDrawable *drawable = NULL;
 
1770
  GimpPixelRgn pixel_rgn;
 
1771
  gint32 iter;
 
1772
  fpos_t tmpfpos;
 
1773
  int red_chan, grn_chan, blu_chan, alpha_chan, ichan;
 
1774
 
 
1775
  IFDBG printf("------- %s ---------------------------------\n",name);
 
1776
 
 
1777
  fd = fopen (name, "rb");
 
1778
  if (! fd)
 
1779
    {
 
1780
      g_message (_("Could not open '%s' for reading: %s"),
 
1781
                 gimp_filename_to_utf8 (name), g_strerror (errno));
 
1782
      return -1;
 
1783
    }
 
1784
 
 
1785
  name_buf = g_strdup_printf (_("Opening '%s'..."),
 
1786
                              gimp_filename_to_utf8 (name));
 
1787
  gimp_progress_init (name_buf);
 
1788
  g_free (name_buf);
 
1789
 
 
1790
  read_whole_file (fd);
 
1791
 
 
1792
  if (psd_image.num_layers > 0) /* PS3-style */
 
1793
    {
 
1794
      int lnum;
 
1795
      GimpImageBaseType gimagetype;
 
1796
 
 
1797
      gimagetype = psd_mode_to_gimp_base_type(PSDheader.mode);
 
1798
      image_ID =
 
1799
        gimp_image_new (PSDheader.columns, PSDheader.rows, gimagetype);
 
1800
      gimp_image_set_filename (image_ID, name);
 
1801
 
 
1802
      if (psd_image.resolution_is_set)
 
1803
        {
 
1804
          gimp_image_set_resolution(image_ID,
 
1805
                                    psd_image.resolution.hRes / 65536.0,
 
1806
                                    psd_image.resolution.vRes / 65536.0);
 
1807
          /* currently can only set one unit for the image so we use the
 
1808
             horizontal unit from the psd image */
 
1809
          gimp_image_set_unit(image_ID,
 
1810
                              psd_unit_to_gimp_unit (psd_image.resolution.widthUnit));
 
1811
        }
 
1812
 
 
1813
      fgetpos (fd, &tmpfpos);
 
1814
 
 
1815
      for (lnum = 0; lnum < psd_image.num_layers; lnum++)
 
1816
        {
 
1817
          gint numc;
 
1818
          guchar* merged_data = NULL;
 
1819
          PSDlayer *layer = psd_image.layer + lnum;
 
1820
 
 
1821
          /*
 
1822
           * since ps supports sloppy bounding boxes it is possible to
 
1823
           * have a 0x0 or Xx0 or 0xY layer.  Gimp doesn't support a
 
1824
           * 0x0 layer so we will just skip these.  We might be able
 
1825
           * to do something better here.
 
1826
           */
 
1827
          if ((layer->width == 0) || (layer->height == 0))
 
1828
            {
 
1829
              IFDBG printf("(bad layer dimensions -- skipping)");
 
1830
              continue;
 
1831
            }
 
1832
          numc = layer->num_channels;
 
1833
 
 
1834
          IFDBG printf("Hey, it's a LAYER with %d channels!\n", numc);
 
1835
 
 
1836
          switch (gimagetype)
 
1837
            {
 
1838
            case GIMP_GRAY:
 
1839
              {
 
1840
                IFDBG printf("It's GRAY.\n");
 
1841
                if (!psd_layer_has_alpha(layer))
 
1842
                  {
 
1843
                    merged_data = g_malloc(layer->width * layer->height);
 
1844
                    seek_to_and_unpack_pixeldata(fd, lnum, 0);
 
1845
                    memcpy(merged_data, layer->channel[0].data,
 
1846
                           layer->width * layer->height);
 
1847
 
 
1848
                    g_free(layer->channel[0].data);
 
1849
                  }
 
1850
                else
 
1851
                  {
 
1852
                    seek_to_and_unpack_pixeldata(fd, lnum, 0);
 
1853
                    seek_to_and_unpack_pixeldata(fd, lnum, 1);
 
1854
                    merged_data =
 
1855
                      chans_to_GRAYA (layer->channel[1].data,
 
1856
                                      layer->channel[0].data,
 
1857
                                      layer->width * layer->height);
 
1858
 
 
1859
                    g_free(layer->channel[0].data);
 
1860
                    g_free(layer->channel[1].data);
 
1861
                  }
 
1862
 
 
1863
                layer_ID = gimp_layer_new (image_ID,
 
1864
                                           layer->name,
 
1865
                                           layer->width,
 
1866
                                           layer->height,
 
1867
                                           (numc==1) ? GIMP_GRAY_IMAGE : GIMP_GRAYA_IMAGE,
 
1868
                                           (100.0 * layer->opacity) / 255.0,
 
1869
                                           psd_lmode_to_gimp_lmode(layer->blendkey));
 
1870
 
 
1871
              }; break; /* case GIMP_GRAY */
 
1872
 
 
1873
            case GIMP_RGB:
 
1874
              {
 
1875
                IFDBG printf("It's RGB, %dx%d.\n", layer->width,
 
1876
                             layer->height);
 
1877
                if (!psd_layer_has_alpha(layer))
 
1878
                  {
 
1879
                    seek_to_and_unpack_pixeldata(fd, lnum, 0);
 
1880
                    seek_to_and_unpack_pixeldata(fd, lnum, 1);
 
1881
                    seek_to_and_unpack_pixeldata(fd, lnum, 2);
 
1882
                    merged_data =
 
1883
                      chans_to_RGB (layer->channel[0].data,
 
1884
                                    layer->channel[1].data,
 
1885
                                    layer->channel[2].data,
 
1886
                                    layer->width *
 
1887
                                    layer->height);
 
1888
 
 
1889
                    g_free(layer->channel[0].data);
 
1890
                    g_free(layer->channel[1].data);
 
1891
                    g_free(layer->channel[2].data);
 
1892
 
 
1893
                    IFDBG
 
1894
                      fprintf(stderr, "YAH0a\n");
 
1895
                  }
 
1896
                else
 
1897
                  {
 
1898
                    seek_to_and_unpack_pixeldata(fd, lnum, 0);
 
1899
                    seek_to_and_unpack_pixeldata(fd, lnum, 1);
 
1900
                    seek_to_and_unpack_pixeldata(fd, lnum, 2);
 
1901
                    seek_to_and_unpack_pixeldata(fd, lnum, 3);
 
1902
 
 
1903
                    /* Fix for unexpected layer data order for files
 
1904
                     * from PS files created by PanoTools. Rather
 
1905
                     * than assuming an order, we find the actual order.
 
1906
                     */
 
1907
 
 
1908
                    red_chan = grn_chan = blu_chan = alpha_chan = -1;
 
1909
 
 
1910
                    for (ichan=0; ichan<numc; ichan++)
 
1911
                      {
 
1912
                        switch(psd_image.layer[lnum].channel[ichan].type)
 
1913
                          {
 
1914
                          case 0:    red_chan = ichan; break;
 
1915
                          case 1:    grn_chan = ichan; break;
 
1916
                          case 2:    blu_chan = ichan; break;
 
1917
                          case -1: alpha_chan = ichan; break;
 
1918
                      }
 
1919
                    }
 
1920
 
 
1921
                    if ((red_chan < 0) ||
 
1922
                        (grn_chan < 0) ||
 
1923
                        (blu_chan < 0) ||
 
1924
                        (alpha_chan < 0))
 
1925
                      {
 
1926
                        g_message ("Error: Cannot identify required RGBA channels");
 
1927
                        gimp_quit();
 
1928
                        break;
 
1929
                      }
 
1930
 
 
1931
                    merged_data =
 
1932
                      chans_to_RGBA (psd_image.layer[lnum].channel[red_chan].data,
 
1933
                                     psd_image.layer[lnum].channel[grn_chan].data,
 
1934
                                     psd_image.layer[lnum].channel[blu_chan].data,
 
1935
                                     psd_image.layer[lnum].channel[alpha_chan].data,
 
1936
                                     psd_image.layer[lnum].width *
 
1937
                                     psd_image.layer[lnum].height);
 
1938
 
 
1939
                    g_free(layer->channel[0].data);
 
1940
                    g_free(layer->channel[1].data);
 
1941
                    g_free(layer->channel[2].data);
 
1942
                    g_free(layer->channel[3].data);
 
1943
 
 
1944
                    IFDBG
 
1945
                      fprintf(stderr, "YAH0b\n");
 
1946
                  }
 
1947
 
 
1948
                IFDBG
 
1949
                  fprintf(stderr, "YAH1\n");
 
1950
 
 
1951
                layer_ID = gimp_layer_new (image_ID,
 
1952
                                           psd_image.layer[lnum].name,
 
1953
                                           psd_image.layer[lnum].width,
 
1954
                                           psd_image.layer[lnum].height,
 
1955
                                           psd_layer_has_alpha(&psd_image.layer[lnum]) ?
 
1956
                                           GIMP_RGBA_IMAGE : GIMP_RGB_IMAGE,
 
1957
                                           (100.0 * (double)psd_image.layer[lnum].opacity) / 255.0,
 
1958
                                           psd_lmode_to_gimp_lmode(psd_image.layer[lnum].blendkey));
 
1959
 
 
1960
                IFDBG
 
1961
                  fprintf(stderr, "YAH2\n");
 
1962
 
 
1963
              }; break; /* case GIMP_RGB */
 
1964
 
 
1965
            default:
 
1966
              {
 
1967
                g_message ("Error: Sorry, can't deal with a layered image of this type.\n");
 
1968
                gimp_quit();
 
1969
              }; break; /* default */
 
1970
            }
 
1971
 
 
1972
          gimp_image_add_layer (image_ID, layer_ID, 0);
 
1973
 
 
1974
          IFDBG
 
1975
            fprintf(stderr, "YAH3\n");
 
1976
 
 
1977
          /* Do a layer mask if it exists */
 
1978
          for (iter = 0; iter < layer->num_channels; iter++)
 
1979
            {
 
1980
              if (layer->channel[iter].type == -2) /* is mask */
 
1981
                {
 
1982
                  gint32 mask_id;
 
1983
                  guchar* lm_data;
 
1984
 
 
1985
                  IFDBG
 
1986
                    fprintf(stderr, "YAH3m\n");
 
1987
 
 
1988
                  lm_data = g_malloc(layer->width * layer->height);
 
1989
                  {
 
1990
#if PANOTOOLS_FIX
 
1991
                    guchar *tmp;
 
1992
#endif
 
1993
 
 
1994
                    seek_to_and_unpack_pixeldata(fd, lnum, iter);
 
1995
                    /* PS layer masks can be a different size to
 
1996
                       their owning layer, so we have to resize them. */
 
1997
                    resize_mask(layer->channel[iter].data,
 
1998
                                lm_data,
 
1999
                                layer->lm_x - layer->x,
 
2000
                                layer->lm_y - layer->y,
 
2001
                                layer->lm_width, layer->lm_height,
 
2002
                                layer->width, layer->height);
 
2003
 
 
2004
                    /* won't be needing the original data any more */
 
2005
                    g_free(layer->channel[iter].data);
 
2006
 
 
2007
                    /* give it to GIMP */
 
2008
                    mask_id = gimp_layer_create_mask(layer_ID, 0);
 
2009
 
 
2010
#if PANOTOOLS_FIX
 
2011
                    /* Convert the layer RGB data (not the mask) to RGBA */
 
2012
                    tmp = merged_data;
 
2013
                    merged_data = RGB_to_RGBA (tmp,
 
2014
                                               psd_image.layer[lnum].width *
 
2015
                                               psd_image.layer[lnum].height);
 
2016
                    g_free (tmp);
 
2017
 
 
2018
                    /* Add alpha - otherwise cannot add layer mask */
 
2019
                    gimp_layer_add_alpha (layer_ID);
 
2020
 
 
2021
#endif /* PANOTOOLS_FIX */
 
2022
                    /* Add layer mask */
 
2023
                    gimp_layer_add_mask (layer_ID, mask_id);
 
2024
 
 
2025
                    drawable = gimp_drawable_get (mask_id);
 
2026
 
 
2027
                    gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0,
 
2028
                                         layer->width, layer->height,
 
2029
                                         TRUE, FALSE);
 
2030
                    gimp_pixel_rgn_set_rect (&pixel_rgn, lm_data, 0, 0,
 
2031
                                             layer->width, layer->height);
 
2032
                  }
 
2033
                  g_free(lm_data);
 
2034
 
 
2035
                  gimp_drawable_flush (drawable);
 
2036
                  gimp_drawable_detach (drawable);
 
2037
                }
 
2038
            }
 
2039
 
 
2040
          IFDBG
 
2041
            fprintf(stderr, "YAH4\n");
 
2042
 
 
2043
          gimp_layer_translate (layer_ID, layer->x, layer->y);
 
2044
 
 
2045
          gimp_layer_set_preserve_trans (layer_ID, layer->protecttrans);
 
2046
          gimp_drawable_set_visible (layer_ID, layer->visible);
 
2047
 
 
2048
          drawable = gimp_drawable_get (layer_ID);
 
2049
 
 
2050
          IFDBG
 
2051
            fprintf(stderr, "YAH5 - merged_data=%p, drawable=%p, drawdim=%dx%dx%d\n",
 
2052
                    merged_data,
 
2053
                    drawable,
 
2054
                    drawable->width,
 
2055
                    drawable->height,
 
2056
                    drawable->bpp);
 
2057
 
 
2058
          gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0,
 
2059
                               layer->width, layer->height,
 
2060
                               TRUE, FALSE);
 
2061
          gimp_pixel_rgn_set_rect (&pixel_rgn, merged_data, 0, 0,
 
2062
                                   layer->width, layer->height);
 
2063
 
 
2064
          IFDBG
 
2065
            fprintf(stderr, "YAH6\n");
 
2066
 
 
2067
          gimp_drawable_flush (drawable);
 
2068
          gimp_drawable_detach (drawable);
 
2069
          drawable = NULL;
 
2070
 
 
2071
          g_free (merged_data);
 
2072
 
 
2073
          gimp_progress_update ((double)(lnum+1.0) /
 
2074
                                (double)psd_image.num_layers);
 
2075
        }
 
2076
      fsetpos (fd, &tmpfpos);
 
2077
    }
 
2078
 
 
2079
  if ((psd_image.num_aux_channels > 0) &&
 
2080
      (psd_image.num_layers > 0))
 
2081
    {
 
2082
      want_aux = TRUE;
 
2083
      IFDBG printf("::::::::::: WANT AUX :::::::::::::::::::::::::::::::::::::::\n");
 
2084
    }
 
2085
  else
 
2086
    {
 
2087
      want_aux = FALSE;
 
2088
    }
 
2089
 
 
2090
 
 
2091
  if (want_aux || (psd_image.num_layers==0)) /* Photoshop2-style: NO LAYERS. */
 
2092
    {
 
2093
 
 
2094
      IFDBG printf("Image data %ld chars\n", PSDheader.imgdatalen);
 
2095
 
 
2096
      step = PSDheader.channels;
 
2097
 
 
2098
      imagetype = PSD_UNKNOWN_IMAGE;
 
2099
     switch (PSDheader.mode)
 
2100
        {
 
2101
        case 0:         /* Bitmap */
 
2102
          imagetype = PSD_BITMAP_IMAGE;
 
2103
          break;
 
2104
        case 1:         /* Grayscale */
 
2105
          imagetype = PSD_GRAY_IMAGE;
 
2106
          break;
 
2107
        case 2:         /* Indexed Colour */
 
2108
          imagetype = PSD_INDEXED_IMAGE;
 
2109
          break;
 
2110
        case 3:         /* RGB Colour */
 
2111
          imagetype = PSD_RGB_IMAGE;
 
2112
          break;
 
2113
        case 4:         /* CMYK Colour */
 
2114
          cmyk = TRUE;
 
2115
          switch (PSDheader.channels)
 
2116
            {
 
2117
            case 4:
 
2118
              imagetype = PSD_RGB_IMAGE;
 
2119
              break;
 
2120
            case 5:
 
2121
              imagetype = PSD_RGBA_IMAGE;
 
2122
              break;
 
2123
            default:
 
2124
              printf("%s: cannot handle CMYK with more than 5 channels\n",
 
2125
                     prog_name);
 
2126
              return(-1);
 
2127
              break;
 
2128
            }
 
2129
          break;
 
2130
        case 7:         /* Multichannel (?) */
 
2131
        case 8:         /* Duotone */
 
2132
        case 9:         /* Lab Colour */
 
2133
        default:
 
2134
          break;
 
2135
        }
 
2136
 
 
2137
 
 
2138
      if (imagetype == PSD_UNKNOWN_IMAGE)
 
2139
        {
 
2140
          printf("%s: Image type %d (%s) is not supported in this data format\n",
 
2141
                 prog_name, PSDheader.mode,
 
2142
                 (PSDheader.mode > 10) ?
 
2143
                 "<out of range>" : modename[PSDheader.mode]);
 
2144
 
 
2145
          return(-1);
 
2146
        }
 
2147
 
 
2148
      if ((PSDheader.bpp != 8) && (PSDheader.bpp != 1))
 
2149
        {
 
2150
          printf("%s: The GIMP only supports 8-bit or 1-bit deep PSD images "
 
2151
                 "at this time.\n",
 
2152
                 prog_name);
 
2153
          return(-1);
 
2154
        }
 
2155
 
 
2156
      IFDBG printf(
 
2157
             "psd:%d gimp:%d gimpbase:%d\n",
 
2158
             imagetype,
 
2159
             psd_type_to_gimp_type(imagetype),
 
2160
             psd_type_to_gimp_base_type(imagetype)
 
2161
             );
 
2162
 
 
2163
 
 
2164
      if (!want_aux)
 
2165
        {
 
2166
          /* gimp doesn't like 0 width/height drawables. */
 
2167
          if ((PSDheader.columns == 0) || (PSDheader.rows == 0))
 
2168
            {
 
2169
              IFDBG printf("(bad psd2-style image dimensions -- skipping)");
 
2170
              image_ID = -1;
 
2171
              goto finish_up;
 
2172
            }
 
2173
 
 
2174
          image_ID = gimp_image_new (PSDheader.columns, PSDheader.rows,
 
2175
                                     psd_type_to_gimp_base_type(imagetype));
 
2176
          gimp_image_set_filename (image_ID, name);
 
2177
          if (psd_type_to_gimp_base_type(imagetype) == GIMP_INDEXED)
 
2178
            {
 
2179
              if ((psd_image.colmaplen%3)!=0)
 
2180
                printf("PSD: Colourmap looks screwed! Aiee!\n");
 
2181
              if (psd_image.colmaplen==0)
 
2182
                printf("PSD: Indexed image has no colourmap!\n");
 
2183
              if (psd_image.colmaplen!=768)
 
2184
                printf("PSD: Warning: Indexed image is %ld!=256 colours.\n",
 
2185
                       psd_image.colmaplen/3);
 
2186
              if (psd_image.colmaplen==768)
 
2187
                {
 
2188
                  reshuffle_cmap(psd_image.colmapdata);
 
2189
                  gimp_image_set_colormap (image_ID,
 
2190
                                           psd_image.colmapdata,
 
2191
                                           256);
 
2192
                }
 
2193
            }
 
2194
 
 
2195
          layer_ID = gimp_layer_new (image_ID, _("Background"),
 
2196
                                     PSDheader.columns, PSDheader.rows,
 
2197
                                     psd_type_to_gimp_type(imagetype),
 
2198
                                     100, GIMP_NORMAL_MODE);
 
2199
 
 
2200
          gimp_image_add_layer (image_ID, layer_ID, 0);
 
2201
          drawable = gimp_drawable_get (layer_ID);
 
2202
 
 
2203
        }
 
2204
 
 
2205
 
 
2206
      if (want_aux)
 
2207
        {
 
2208
          switch (PSDheader.mode)
 
2209
            {
 
2210
            case 1:             /* Grayscale */
 
2211
              channels = 1;
 
2212
              break;
 
2213
            case 2:             /* Indexed Colour */
 
2214
              channels = 1;
 
2215
              break;
 
2216
            case 3:             /* RGB Colour */
 
2217
              channels = 3;
 
2218
              break;
 
2219
            default:
 
2220
              printf("aux? Aieeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee!!!!!!!!!\n");
 
2221
              channels = 1;
 
2222
              break;
 
2223
            }
 
2224
 
 
2225
          channels = PSDheader.channels - channels;
 
2226
 
 
2227
          if (psd_image.absolute_alpha)
 
2228
            {
 
2229
              channels--;
 
2230
            }
 
2231
        }
 
2232
      else
 
2233
        {
 
2234
          channels = gimp_drawable_bpp(drawable->drawable_id);
 
2235
        }
 
2236
 
 
2237
 
 
2238
      dest = g_malloc( step * PSDheader.columns * PSDheader.rows );
 
2239
 
 
2240
      if (PSDheader.compression == 1)
 
2241
        {
 
2242
          nguchars = PSDheader.columns * PSDheader.rows;
 
2243
          temp = g_malloc (PSDheader.imgdatalen);
 
2244
          xfread (fd, temp, PSDheader.imgdatalen, "image data");
 
2245
          if (!cmyk)
 
2246
            {
 
2247
              gimp_progress_update ((double)1.00);
 
2248
 
 
2249
              if(imagetype==PSD_BITMAP_IMAGE) /* convert bitmap to grayscale */
 
2250
                {
 
2251
                  guchar *monobuf;
 
2252
 
 
2253
                  monobuf =
 
2254
                    g_malloc (((PSDheader.columns + 7) >> 3) * PSDheader.rows);
 
2255
 
 
2256
                  decode (PSDheader.imgdatalen,
 
2257
                          nguchars >> 3, temp,monobuf, step);
 
2258
                  bitmap2gray (monobuf, dest,
 
2259
                               PSDheader.columns, PSDheader.rows);
 
2260
 
 
2261
                  g_free (monobuf);
 
2262
                }
 
2263
              else
 
2264
                {
 
2265
                  decode(PSDheader.imgdatalen, nguchars, temp, dest, step);
 
2266
                }
 
2267
            }
 
2268
          else
 
2269
            {
 
2270
              gimp_progress_update (1.0);
 
2271
 
 
2272
              cmykbuf = g_malloc (step * nguchars);
 
2273
              decode (PSDheader.imgdatalen, nguchars, temp, cmykbuf, step);
 
2274
 
 
2275
              cmyk2rgb (cmykbuf, dest, PSDheader.columns, PSDheader.rows,
 
2276
                        step > 4);
 
2277
              g_free (cmykbuf);
 
2278
            }
 
2279
 
 
2280
          g_free (temp);
 
2281
        }
 
2282
      else
 
2283
        {
 
2284
          if (!cmyk)
 
2285
            {
 
2286
              gimp_progress_update ((double)1.00);
 
2287
 
 
2288
              xfread_interlaced(fd, dest, PSDheader.imgdatalen,
 
2289
                                "raw image data", step);
 
2290
            }
 
2291
          else
 
2292
            {
 
2293
              gimp_progress_update ((double)1.00);
 
2294
 
 
2295
              cmykbuf = g_malloc(PSDheader.imgdatalen);
 
2296
              xfread_interlaced(fd, cmykbuf, PSDheader.imgdatalen,
 
2297
                                "raw cmyk image data", step);
 
2298
 
 
2299
              cmykp2rgb(cmykbuf, dest,
 
2300
                        PSDheader.columns, PSDheader.rows, step > 4);
 
2301
              g_free(cmykbuf);
 
2302
            }
 
2303
        }
 
2304
 
 
2305
 
 
2306
      if (want_aux) /* want_aux */
 
2307
        {
 
2308
          extract_channels(dest, channels, step,
 
2309
                           image_ID,
 
2310
                           PSDheader.columns, PSDheader.rows);
 
2311
 
 
2312
          goto finish_up; /* Haha!  Look!  A goto! */
 
2313
        }
 
2314
      else
 
2315
        {
 
2316
          gimp_progress_update ((double)1.00);
 
2317
 
 
2318
          if (channels == step) /* gimp bpp == psd bpp */
 
2319
            {
 
2320
 
 
2321
              if (psd_type_to_gimp_type(imagetype)==GIMP_INDEXEDA_IMAGE)
 
2322
                {
 
2323
                  printf("@@@@ Didn't know that this could happen...\n");
 
2324
                  for (iter=0; iter<drawable->width*drawable->height; iter++)
 
2325
                    {
 
2326
                      dest[iter*2+1] = 255;
 
2327
                    }
 
2328
                }
 
2329
 
 
2330
              gimp_pixel_rgn_init (&pixel_rgn, drawable,
 
2331
                                   0, 0, drawable->width, drawable->height,
 
2332
                                   TRUE, FALSE);
 
2333
              gimp_pixel_rgn_set_rect (&pixel_rgn, dest,
 
2334
                                       0, 0, drawable->width, drawable->height);
 
2335
 
 
2336
              gimp_drawable_flush (drawable);
 
2337
              gimp_drawable_detach (drawable);
 
2338
            }
 
2339
          else
 
2340
            {
 
2341
              IFDBG printf("Uhhh... uhm... extra channels... heavy...\n");
 
2342
 
 
2343
              extract_data_and_channels(dest, channels, step,
 
2344
                                        image_ID, drawable,
 
2345
                                        drawable->width, drawable->height);
 
2346
            }
 
2347
        }
 
2348
 
 
2349
 
 
2350
 
 
2351
    finish_up:
 
2352
 
 
2353
      g_free (dest);
 
2354
 
 
2355
      if (psd_image.colmaplen > 0)
 
2356
        g_free(psd_image.colmapdata);
 
2357
    }
 
2358
 
 
2359
  if (psd_image.num_guides > 0)
 
2360
    {
 
2361
      PSDguide *guide = psd_image.guides;
 
2362
      int i;
 
2363
 
 
2364
      IFDBG printf("--- Adding %d Guides\n", psd_image.num_guides);
 
2365
 
 
2366
      for (i = 0; i < psd_image.num_guides; i++, guide++)
 
2367
        {
 
2368
          if (guide->horizontal)
 
2369
            gimp_image_add_hguide (image_ID, guide->position);
 
2370
          else
 
2371
            gimp_image_add_vguide (image_ID, guide->position);
 
2372
        }
 
2373
    }
 
2374
 
 
2375
  gimp_displays_flush();
 
2376
 
 
2377
  IFDBG printf("--- %d layers : pos %ld : a-alph %d ---\n",
 
2378
         psd_image.num_layers, (long int)ftell(fd),
 
2379
         psd_image.absolute_alpha);
 
2380
 
 
2381
  return image_ID;
 
2382
}
 
2383
 
 
2384
static void
 
2385
decode(long clen, long uclen, guchar *src, guchar* dst, int step)
 
2386
{
 
2387
    gint i, j;
 
2388
    gint32 l;
 
2389
    gushort * w;
 
2390
 
 
2391
    l = clen;
 
2392
    for (i = 0; i < PSDheader.rows*PSDheader.channels; ++i)
 
2393
      {
 
2394
        l -= PSDheader.rowlength[i];
 
2395
      }
 
2396
    if (l)
 
2397
      {
 
2398
        g_warning("decode: %ld should be zero\n", (long)l);
 
2399
      }
 
2400
 
 
2401
    w = PSDheader.rowlength;
 
2402
 
 
2403
    packbitsdecode(&clen, uclen, src, dst++, step);
 
2404
 
 
2405
    for (j = 0; j < step-1; ++j)
 
2406
      {
 
2407
        for (i = 0; i < PSDheader.rows; ++i)
 
2408
          {
 
2409
            src += *w++;
 
2410
          }
 
2411
        packbitsdecode(&clen, uclen, src, dst++, step);
 
2412
      }
 
2413
 
 
2414
    IFDBG printf("clen %ld\n", clen);
 
2415
}
 
2416
 
 
2417
/*
 
2418
 * Decode a PackBits data stream.
 
2419
 */
 
2420
static void
 
2421
packbitsdecode(long * clenp, long uclen, guchar *src, guchar *dst, int step)
 
2422
{
 
2423
    gint n, b;
 
2424
    gint32 clen = *clenp;
 
2425
 
 
2426
    while ((clen > 0) && (uclen > 0)) {
 
2427
        n = (int) *src++;
 
2428
        if (n >= 128)
 
2429
            n -= 256;
 
2430
        if (n < 0) {            /* replicate next guchar -n+1 times */
 
2431
            clen -= 2;
 
2432
            if (n == -128)      /* nop */
 
2433
                continue;
 
2434
            n = -n + 1;
 
2435
            uclen -= n;
 
2436
            for (b = *src++; n > 0; --n) {
 
2437
                *dst = b;
 
2438
                dst += step;
 
2439
            }
 
2440
        } else {                /* copy next n+1 guchars literally */
 
2441
            for (b = ++n; b > 0; --b) {
 
2442
                *dst = *src++;
 
2443
                dst += step;
 
2444
            }
 
2445
            uclen -= n;
 
2446
            clen -= n+1;
 
2447
        }
 
2448
    }
 
2449
    if (uclen > 0) {
 
2450
        printf("%s: unexpected EOF while reading image data\n", prog_name);
 
2451
        gimp_quit();
 
2452
    }
 
2453
    *clenp = clen;
 
2454
}
 
2455
 
 
2456
/*
 
2457
 * Decode a PackBits channel from file.
 
2458
 */
 
2459
static void
 
2460
unpack_pb_channel(FILE *fd, guchar *dst, gint32 unpackedlen, guint32 *offset)
 
2461
{
 
2462
    int n, b;
 
2463
    gint32 upremain = unpackedlen;
 
2464
 
 
2465
    while (upremain > 0)
 
2466
      {
 
2467
        n = (int) getguchar(fd, "packbits1");
 
2468
        (*offset)++;
 
2469
 
 
2470
        if (n >= 128)
 
2471
          n -= 256;
 
2472
        if (n < 0)
 
2473
          {             /* replicate next guchar -n+1 times */
 
2474
            if (n == -128)      /* nop */
 
2475
              continue;
 
2476
            n = -n + 1;
 
2477
            /*      upremain -= n;*/
 
2478
 
 
2479
            b = getguchar(fd, "packbits2");
 
2480
            (*offset)++;
 
2481
            for (; n > 0; --n)
 
2482
              {
 
2483
                if (upremain >= 0) {
 
2484
                  *dst = b;
 
2485
                  dst ++;
 
2486
                }
 
2487
                upremain--;
 
2488
              }
 
2489
          }
 
2490
        else
 
2491
          {             /* copy next n+1 guchars literally */
 
2492
            for (b = ++n; b > 0; --b)
 
2493
              {
 
2494
                const guchar c = getguchar(fd, "packbits3");
 
2495
                if (upremain >= 0) {
 
2496
                  *dst = c;
 
2497
                  dst ++;
 
2498
                }
 
2499
                (*offset)++;
 
2500
                upremain--;
 
2501
              }
 
2502
            /*      upremain -= n;*/
 
2503
          }
 
2504
      }
 
2505
 
 
2506
    if (upremain < 0)
 
2507
      {
 
2508
        printf("*** Unpacking overshot destination (%d) buffer by %d bytes!\n",
 
2509
               unpackedlen,
 
2510
               -upremain);
 
2511
      }
 
2512
}
 
2513
 
 
2514
static void
 
2515
cmyk2rgb(unsigned char * src, unsigned char * dst,
 
2516
         long width, long height, int alpha)
 
2517
{
 
2518
    int r, g, b, k;
 
2519
    int i, j;
 
2520
 
 
2521
 
 
2522
    for (i = 0; i < height; i++) {
 
2523
        for (j = 0; j < width; j++) {
 
2524
            r = *src++;
 
2525
            g = *src++;
 
2526
            b = *src++;
 
2527
            k = *src++;
 
2528
 
 
2529
            gimp_cmyk_to_rgb_int (&r, &g, &b, &k);
 
2530
 
 
2531
            *dst++ = r;
 
2532
            *dst++ = g;
 
2533
            *dst++ = b;
 
2534
 
 
2535
            if (alpha)
 
2536
                *dst++ = *src++;
 
2537
        }
 
2538
 
 
2539
        if ((i % 5) == 0)
 
2540
          gimp_progress_update ((double)(2.0*(double)height)/
 
2541
                                ((double)height+(double)i));
 
2542
    }
 
2543
}
 
2544
 
 
2545
/*
 
2546
 * Decode planar CMYK(A) to RGB(A).
 
2547
 */
 
2548
static void
 
2549
cmykp2rgb(unsigned char * src, unsigned char * dst,
 
2550
          long width, long height, int alpha)
 
2551
{
 
2552
    int r, g, b, k;
 
2553
    int i, j;
 
2554
    long n;
 
2555
    guchar *rp, *gp, *bp, *kp, *ap;
 
2556
 
 
2557
    n = width * height;
 
2558
    rp = src;
 
2559
    gp = rp + n;
 
2560
    bp = gp + n;
 
2561
    kp = bp + n;
 
2562
    ap = kp + n;
 
2563
 
 
2564
    for (i = 0; i < height; i++) {
 
2565
        for (j = 0; j < width; j++) {
 
2566
            r = *rp++;
 
2567
            g = *gp++;
 
2568
            b = *bp++;
 
2569
            k = *kp++;
 
2570
 
 
2571
            gimp_cmyk_to_rgb_int (&r, &g, &b, &k);
 
2572
 
 
2573
            *dst++ = r;
 
2574
            *dst++ = g;
 
2575
            *dst++ = b;
 
2576
 
 
2577
            if (alpha)
 
2578
                *dst++ = *ap++;
 
2579
        }
 
2580
        if ((i % 5) == 0)
 
2581
          gimp_progress_update ((double)(2.0*(double)height)/
 
2582
                                ((double)height+(double)i));
 
2583
    }
 
2584
}
 
2585
 
 
2586
static void
 
2587
bitmap2gray(guchar *src,guchar *dest,long w,long h)
 
2588
{
 
2589
  int i,j;
 
2590
  for(i=0;i<h;i++)
 
2591
    {
 
2592
      int mask=0x80;
 
2593
      for(j=0;j<w;j++)
 
2594
        {
 
2595
          *dest++=(*src&mask)?0:255;
 
2596
          mask>>=1;
 
2597
          if(!mask)
 
2598
            {
 
2599
              src++;
 
2600
              mask=0x80;
 
2601
            }
 
2602
        }
 
2603
      if(mask!=0x80) src++;
 
2604
    }
 
2605
}
 
2606
 
 
2607
static void
 
2608
dumpchunk(size_t n, FILE * fd, gchar *why)
 
2609
{
 
2610
  guint32 i;
 
2611
 
 
2612
  printf("\n");
 
2613
 
 
2614
  for (i=0;i<n;i++)
 
2615
    {
 
2616
      printf("%02x ",(int)getguchar(fd, why));
 
2617
    }
 
2618
 
 
2619
  printf("\n");
 
2620
}
 
2621
 
 
2622
static void
 
2623
throwchunk(size_t n, FILE * fd, gchar *why)
 
2624
{
 
2625
#if 0
 
2626
  guchar *tmpchunk;
 
2627
 
 
2628
  if (n==0)
 
2629
    {
 
2630
      return;
 
2631
    }
 
2632
 
 
2633
  tmpchunk = g_malloc(n);
 
2634
  xfread(fd, tmpchunk, n, why);
 
2635
  g_free(tmpchunk);
 
2636
#else
 
2637
  if (n==0)
 
2638
    {
 
2639
      return;
 
2640
    }
 
2641
 
 
2642
  if (fseek(fd, n, SEEK_CUR) != 0)
 
2643
    {
 
2644
      printf("%s: unable to seek forward while reading '%s' chunk\n",
 
2645
             prog_name, why);
 
2646
      gimp_quit();
 
2647
    }
 
2648
#endif
 
2649
}
 
2650
 
 
2651
static gchar *
 
2652
getstring(size_t n, FILE * fd, gchar *why)
 
2653
{
 
2654
  gchar *tmpchunk;
 
2655
 
 
2656
  tmpchunk = g_malloc(n + 1);
 
2657
  xfread(fd, tmpchunk, n, why);
 
2658
  tmpchunk[n] = 0;
 
2659
 
 
2660
  return tmpchunk;  /* caller should free memory */
 
2661
}
 
2662
 
 
2663
static gchar *
 
2664
getpascalstring(FILE *fd, gchar *why)
 
2665
{
 
2666
  guchar *tmpchunk;
 
2667
  guchar len;
 
2668
 
 
2669
  xfread(fd, &len, 1, why);
 
2670
 
 
2671
  if (len==0)
 
2672
    {
 
2673
      return NULL;
 
2674
    }
 
2675
 
 
2676
  tmpchunk = g_malloc(len+1);
 
2677
 
 
2678
  xfread(fd, tmpchunk, len, why);
 
2679
  tmpchunk[len]=0;
 
2680
 
 
2681
  return (gchar *) tmpchunk; /* caller should free memory */
 
2682
}
 
2683
 
 
2684
static guchar
 
2685
getguchar(FILE *fd, gchar *why)
 
2686
{
 
2687
  gint tmp;
 
2688
 
 
2689
  tmp = fgetc(fd);
 
2690
 
 
2691
  if (tmp == EOF)
 
2692
    {
 
2693
      printf("%s: unexpected EOF while reading '%s' chunk\n",
 
2694
             prog_name, why);
 
2695
      gimp_quit();
 
2696
    }
 
2697
  return tmp;
 
2698
}
 
2699
 
 
2700
static gshort
 
2701
getgshort(FILE *fd, gchar *why)
 
2702
{
 
2703
  guchar b1, b2;
 
2704
 
 
2705
  b1 = getguchar(fd, why);
 
2706
  b2 = getguchar(fd, why);
 
2707
 
 
2708
  return (gshort) ((b1 * 256) + b2);
 
2709
}
 
2710
 
 
2711
static glong
 
2712
getglong(FILE *fd, gchar *why)
 
2713
{
 
2714
  guchar s1, s2, s3, s4;
 
2715
 
 
2716
  s1 = getguchar(fd, why);
 
2717
  s2 = getguchar(fd, why);
 
2718
  s3 = getguchar(fd, why);
 
2719
  s4 = getguchar(fd, why);
 
2720
 
 
2721
  return (glong) ((s1*256*256*256) + (s2*256*256) + (s3*256) + s4);
 
2722
}
 
2723
 
 
2724
static void
 
2725
xfread(FILE * fd, void * buf, long len, gchar *why)
 
2726
{
 
2727
  if (fread(buf, len, 1, fd) == 0)
 
2728
    {
 
2729
      printf("%s: unexpected EOF while reading '%s' chunk\n",
 
2730
             prog_name, why);
 
2731
      gimp_quit();
 
2732
    }
 
2733
}
 
2734
 
 
2735
static void
 
2736
xfread_interlaced(FILE* fd, guchar* buf, long len, gchar *why, gint step)
 
2737
{
 
2738
  guchar* dest;
 
2739
  gint pix, pos, bpplane;
 
2740
 
 
2741
  bpplane = len/step;
 
2742
 
 
2743
  if (len%step != 0)
 
2744
    {
 
2745
      printf("PSD: Stern warning: data size is not a factor of step size!\n");
 
2746
    }
 
2747
 
 
2748
  for (pix=0; pix<step; pix++)
 
2749
    {
 
2750
      dest = buf + pix;
 
2751
 
 
2752
      for (pos=0; pos<bpplane; pos++)
 
2753
        {
 
2754
          *dest = getguchar(fd, why);
 
2755
          dest += step;
 
2756
        }
 
2757
    }
 
2758
}
 
2759
 
 
2760
static void
 
2761
read_whole_file(FILE * fd)
 
2762
{
 
2763
    guint16 w;
 
2764
    gint32 pos;
 
2765
    gchar dummy[6];
 
2766
    gint i;
 
2767
 
 
2768
    xfread(fd, &PSDheader.signature, 4, "signature");
 
2769
    PSDheader.version = getgshort(fd, "version");
 
2770
    xfread(fd, &dummy, 6, "reserved");
 
2771
    PSDheader.channels = getgshort(fd, "channels");
 
2772
    PSDheader.rows = getglong(fd, "rows");
 
2773
    PSDheader.columns = getglong(fd, "columns");
 
2774
    PSDheader.bpp = getgshort(fd, "depth");
 
2775
    PSDheader.mode = getgshort(fd, "mode");
 
2776
 
 
2777
 
 
2778
    psd_image.num_layers = 0;
 
2779
    psd_image.type = PSDheader.mode;
 
2780
    psd_image.colmaplen = 0;
 
2781
    psd_image.num_aux_channels = 0;
 
2782
    psd_image.num_guides = 0;
 
2783
 
 
2784
 
 
2785
    psd_image.colmaplen = getglong(fd, "color data length");
 
2786
 
 
2787
    if (psd_image.colmaplen > 0)
 
2788
      {
 
2789
        psd_image.colmapdata = g_malloc(psd_image.colmaplen);
 
2790
        xfread(fd, psd_image.colmapdata, psd_image.colmaplen, "colormap");
 
2791
      }
 
2792
 
 
2793
 
 
2794
    PSDheader.imgreslen = getglong(fd, "image resource length");
 
2795
    if (PSDheader.imgreslen > 0)
 
2796
      {
 
2797
        do_image_resources(fd);
 
2798
      }
 
2799
 
 
2800
 
 
2801
    PSDheader.miscsizelen = getglong(fd, "misc size data length");
 
2802
    if (PSDheader.miscsizelen > 0)
 
2803
      {
 
2804
        do_layer_and_mask(fd);
 
2805
      }
 
2806
 
 
2807
 
 
2808
    PSDheader.compression = getgshort(fd, "compression");
 
2809
    IFDBG printf("<<compr:%d>>", (int)PSDheader.compression);
 
2810
    if (PSDheader.compression == 1) /* RLE */
 
2811
      {
 
2812
        PSDheader.rowlength = g_malloc(PSDheader.rows *
 
2813
                                       PSDheader.channels * sizeof(gushort));
 
2814
        for (i = 0; i < PSDheader.rows*PSDheader.channels; ++i)
 
2815
          PSDheader.rowlength[i] = getgshort(fd, "x");
 
2816
      }
 
2817
    pos = ftell(fd);
 
2818
    fseek(fd, 0, SEEK_END);
 
2819
    PSDheader.imgdatalen = ftell(fd)-pos;
 
2820
    fseek(fd, pos, SEEK_SET);
 
2821
 
 
2822
 
 
2823
    if (strncmp(PSDheader.signature, "8BPS", 4) != 0)
 
2824
      {
 
2825
        printf("%s: not an Adobe Photoshop PSD file\n", prog_name);
 
2826
        gimp_quit();
 
2827
      }
 
2828
    if (PSDheader.version != 1)
 
2829
      {
 
2830
        printf("%s: bad version number '%d', not 1\n",
 
2831
               prog_name, PSDheader.version);
 
2832
        gimp_quit();
 
2833
      }
 
2834
    w = PSDheader.mode;
 
2835
    IFDBG printf("HEAD:\n"
 
2836
           "\tChannels %d\n\tRows %ld\n\tColumns %ld\n\tDepth %d\n\tMode %d (%s)\n"
 
2837
           "\tColour data %ld guchars\n",
 
2838
           PSDheader.channels, PSDheader.rows,
 
2839
           PSDheader.columns, PSDheader.bpp,
 
2840
           w, modename[w < 10 ? w : 10],
 
2841
           psd_image.colmaplen);
 
2842
    /*    printf("\tImage resource length: %lu\n", PSDheader.imgreslen);*/
 
2843
    IFDBG printf("\tLayer/Mask Data length: %lu\n", PSDheader.miscsizelen);
 
2844
    w = PSDheader.compression;
 
2845
    IFDBG printf("\tCompression %d (%s)\n", w, w ? "RLE" : "raw");
 
2846
}