~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

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