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

« back to all changes in this revision

Viewing changes to plug-ins/common/psd_save.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:
46
46
 *       memory and opperate on tile-size chunks.  Create a flattened
47
47
 *       copy of the image when necessary. Fixes file corruption bug
48
48
 *       #167139 and memory bug #121871
 
49
 *
 
50
 *  2006-03-29 Guillermo S. Romero <gsr.bugs@infernal-iceberg.com>
 
51
 *       - Added/enabled basic support for layer masks based in psd.c
 
52
 *         and whatever was already here.
 
53
 *         Layers that are not canvas sized need investigation, here
 
54
 *         or in the import plugin something seems wrong.
 
55
 *       - Cosmetic changes about the debug messages, more like psd.c.
49
56
 */
50
57
 
51
58
/*
52
59
 * TODO:
53
60
 *       Save preview
54
 
 *       Save layer masks
55
61
 */
56
62
 
57
63
/*
61
67
#include "config.h"
62
68
 
63
69
#include <errno.h>
64
 
#include <stdio.h>
65
 
#include <stdlib.h>
66
 
#ifdef HAVE_UNISTD_H
67
 
#include <unistd.h>
68
 
#endif
69
70
#include <string.h>
70
71
 
71
 
#include <gtk/gtk.h>
 
72
#include <glib/gstdio.h>
72
73
 
73
74
#include "libgimp/gimp.h"
74
75
#include "libgimp/gimpui.h"
78
79
 
79
80
/* *** DEFINES *** */
80
81
 
 
82
#define SAVE_PROC      "file-psd-save"
 
83
#define PLUG_IN_BINARY "psd_save"
 
84
 
81
85
/* set to TRUE if you want debugging, FALSE otherwise */
82
86
#define DEBUG FALSE
83
87
 
100
104
 
101
105
typedef struct PsdLayerDimension
102
106
{
103
 
  gint left;
104
 
  gint top;
 
107
  gint   left;
 
108
  gint   top;
105
109
  gint32 width;
106
110
  gint32 height;
107
111
} PSD_Layer_Dimension;
138
142
                                    gint             *nreturn_vals,
139
143
                                    GimpParam       **return_vals);
140
144
 
141
 
static void * xmalloc              (size_t n);
142
145
static void   psd_lmode_layer      (gint32 idLayer, gchar* psdMode);
143
146
static void   reshuffle_cmap_write (guchar *mapGimp);
144
147
static void   save_header          (FILE *fd, gint32 image_id);
151
154
static void   write_pascalstring   (FILE *fd, char *val, gint padding,
152
155
                                    gchar *why);
153
156
static void   write_string         (FILE *fd, char *val, gchar *why);
154
 
static void   write_gchar          (FILE *fd, unsigned char val, gchar *why);
155
 
static void   write_gshort         (FILE *fd, gshort val, gchar *why);
156
 
static void   write_glong          (FILE *fd, glong val, gchar *why);
157
 
    
 
157
static void   write_gchar          (FILE *fd, guchar val, gchar *why);
 
158
static void   write_gint16         (FILE *fd, gint16 val, gchar *why);
 
159
static void   write_gint32         (FILE *fd, gint32 val, gchar *why);
 
160
 
158
161
static void   write_pixel_data     (FILE *fd, gint32 drawableID,
159
 
                                    gint32 *ChanLenPosition,
160
 
                                    glong rowlenOffset);
161
 
 
162
 
 
163
 
GimpPlugInInfo PLUG_IN_INFO =
 
162
                                    glong *ChanLenPosition,
 
163
                                    gint32 rowlenOffset);
 
164
 
 
165
 
 
166
const GimpPlugInInfo PLUG_IN_INFO =
164
167
{
165
168
  NULL,    /* init_proc */
166
169
  NULL,    /* quit_proc */
168
171
  run,     /* run_proc */
169
172
};
170
173
 
171
 
static const gchar *prog_name = "PSD";
172
174
 
173
175
MAIN()
174
176
 
176
178
static void
177
179
query (void)
178
180
{
179
 
  static GimpParamDef save_args[] =
 
181
  static const GimpParamDef save_args[] =
180
182
  {
181
 
    { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
182
 
    { GIMP_PDB_IMAGE, "image", "Input image" },
183
 
    { GIMP_PDB_DRAWABLE, "drawable", "Drawable to save" },
184
 
    { GIMP_PDB_STRING, "filename", "The name of the file to save the image in" },
185
 
    { GIMP_PDB_STRING, "raw_filename", "The name of the file to save the image in" },
186
 
    { GIMP_PDB_INT32, "compression", "Compression type: { NONE (0), LZW (1), PACKBITS (2)" },
187
 
    { GIMP_PDB_INT32, "fillorder", "Fill Order: { MSB to LSB (0), LSB to MSB (1)" }
 
183
    { GIMP_PDB_INT32,    "run-mode",     "Interactive, non-interactive" },
 
184
    { GIMP_PDB_IMAGE,    "image",        "Input image" },
 
185
    { GIMP_PDB_DRAWABLE, "drawable",     "Drawable to save" },
 
186
    { GIMP_PDB_STRING,   "filename",     "The name of the file to save the image in" },
 
187
    { GIMP_PDB_STRING,   "raw-filename", "The name of the file to save the image in" },
 
188
    { GIMP_PDB_INT32,    "compression",  "Compression type: { NONE (0), LZW (1), PACKBITS (2)" },
 
189
    { GIMP_PDB_INT32,    "fill-order",   "Fill Order: { MSB to LSB (0), LSB to MSB (1)" }
188
190
  };
189
191
 
190
 
  gimp_install_procedure ("file_psd_save",
 
192
  gimp_install_procedure (SAVE_PROC,
191
193
                          "saves files in the Photoshop(tm) PSD file format",
192
194
                          "This filter saves 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.",
193
195
                          "Monigotes",
199
201
                          G_N_ELEMENTS (save_args), 0,
200
202
                          save_args, NULL);
201
203
 
202
 
  gimp_register_file_handler_mime ("file_psd_save", "image/x-psd");
203
 
  gimp_register_save_handler ("file_psd_save", "psd", "");
 
204
  gimp_register_file_handler_mime (SAVE_PROC, "image/x-psd");
 
205
  gimp_register_save_handler (SAVE_PROC, "psd", "");
204
206
}
205
207
 
206
208
 
207
 
 
208
209
static void
209
210
run (const gchar      *name,
210
211
     gint              nparams,
219
220
 
220
221
  *nreturn_vals = 1;
221
222
  *return_vals  = values;
 
223
 
222
224
  values[0].type          = GIMP_PDB_STATUS;
223
225
  values[0].data.d_status = GIMP_PDB_CALLING_ERROR;
224
226
 
225
 
  if (strcmp (name, "file_psd_save") == 0)
 
227
  if (strcmp (name, SAVE_PROC) == 0)
226
228
    {
227
229
      gint32           image_id;
228
230
      gint32           drawable_id;
229
231
      GimpExportReturn export = GIMP_EXPORT_IGNORE;
230
232
 
231
 
      IFDBG printf ("\n---------------- %s ----------------\n", param[3].data.d_string);
 
233
      IFDBG printf ("\n---------------- %s ----------------\n",
 
234
                    param[3].data.d_string);
232
235
 
233
236
      image_id    = param[1].data.d_int32;
234
237
      drawable_id = param[2].data.d_int32;
235
238
 
236
239
      switch (run_mode)
237
 
        {
238
 
        case GIMP_RUN_INTERACTIVE:
239
 
        case GIMP_RUN_WITH_LAST_VALS:
240
 
          gimp_ui_init ("psd_save", FALSE);
 
240
        {
 
241
        case GIMP_RUN_INTERACTIVE:
 
242
        case GIMP_RUN_WITH_LAST_VALS:
 
243
          gimp_ui_init (PLUG_IN_BINARY, FALSE);
241
244
          export = gimp_export_image (&image_id, &drawable_id, "PSD",
242
245
                                      GIMP_EXPORT_CAN_HANDLE_RGB     |
243
246
                                      GIMP_EXPORT_CAN_HANDLE_GRAY    |
244
247
                                      GIMP_EXPORT_CAN_HANDLE_INDEXED |
245
248
                                      GIMP_EXPORT_CAN_HANDLE_ALPHA   |
246
 
                                      GIMP_EXPORT_CAN_HANDLE_LAYERS);
 
249
                                      GIMP_EXPORT_CAN_HANDLE_LAYERS  |
 
250
                                      GIMP_EXPORT_CAN_HANDLE_LAYER_MASKS);
247
251
 
248
252
          if (export == GIMP_EXPORT_CANCEL)
249
253
            {
266
270
    }
267
271
}
268
272
 
269
 
 
270
 
 
271
 
static void *
272
 
xmalloc (size_t n)
273
 
{
274
 
  void *p;
275
 
 
276
 
  if (n == 0)
277
 
    {
278
 
      IFDBG printf ("PSD: WARNING: %s: xmalloc asked for zero-sized chunk\n", prog_name);
279
 
 
280
 
      return (NULL);
281
 
    }
282
 
 
283
 
  if ((p = g_malloc (n)) != NULL)
284
 
    return p;
285
 
 
286
 
  IFDBG printf ("%s: out of memory\n", prog_name);
287
 
  gimp_quit ();
288
 
  return NULL;
289
 
}
290
 
 
291
273
static void
292
274
psd_lmode_layer (gint32 idLayer, gchar *psdMode)
293
275
{
326
308
    case GIMP_VALUE_MODE:                  /* ? */
327
309
      strcpy (psdMode, "lum ");
328
310
      break;
329
 
    case GIMP_OVERLAY_MODE:                /* ? */
 
311
    case GIMP_HARDLIGHT_MODE:
 
312
      strcpy (psdMode, "hLit");
 
313
      break;
 
314
    case GIMP_SOFTLIGHT_MODE:
 
315
      strcpy (psdMode, "sLit");
 
316
      break;
 
317
    case GIMP_OVERLAY_MODE:
330
318
      strcpy (psdMode, "over");
331
319
      break;
332
 
/*    case GIMP_BEHIND_MODE:                 These are from GIMP 1.1.14*/
333
 
/*    case GIMP_DIVIDE_MODE:                 These are from GIMP 1.1.14*/
334
 
    case GIMP_ADDITION_MODE:
335
 
    case GIMP_SUBTRACT_MODE:
336
 
      IFDBG printf ("PSD: Warning - unsupported layer-blend mode: %c, using 'norm' mode\n",
337
 
                    gimp_layer_get_mode (idLayer));
338
 
      strcpy (psdMode, "norm");
339
 
      break;
340
320
    default:
341
 
      IFDBG printf ("PSD: Warning - UNKNOWN layer-blend mode, reverting to 'norm'\n");
342
 
      strcpy (psdMode, "norm");
 
321
      {
 
322
        const gchar *nick = "?";
 
323
 
 
324
        gimp_enum_get_value (GIMP_TYPE_LAYER_MODE_EFFECTS,
 
325
                             gimp_layer_get_mode (idLayer),
 
326
                             NULL, &nick, NULL, NULL);
 
327
 
 
328
        g_message (_("Unable to save layer with mode '%s'.  Either the PSD "
 
329
                     "file format or the save plug-in does not support that, "
 
330
                     "using normal mode instead."), nick);
 
331
 
 
332
        IFDBG printf ("PSD: Warning - unsupported layer-blend mode: %s, "
 
333
                      "using normal mode\n", nick);
 
334
 
 
335
        strcpy (psdMode, "norm");
 
336
      }
343
337
      break;
344
338
    }
345
339
}
371
365
      xfwrite (fd, val, len, why);
372
366
    }
373
367
  else
374
 
    write_gshort (fd, 0, why);
 
368
    write_gint16 (fd, 0, why);
375
369
 
376
370
  /* If total length (length byte + content) is not a multiple of PADDING,
377
371
     add zeros to pad it.  */
400
394
 
401
395
 
402
396
static void
403
 
write_gchar (FILE *fd, unsigned char val, gchar *why)
 
397
write_gchar (FILE *fd, guchar val, gchar *why)
404
398
{
405
399
  unsigned char b[2];
406
 
  gint32 pos;
 
400
  glong         pos;
407
401
 
408
402
  b[0] = val;
409
403
  b[1] = 0;
421
415
 
422
416
 
423
417
static void
424
 
write_gshort (FILE *fd, gshort val, gchar *why)
 
418
write_gint16 (FILE *fd, gint16 val, gchar *why)
425
419
{
426
420
  unsigned char b[2];
427
421
  /*  b[0] = val & 255;
432
426
 
433
427
  if (fwrite (&b, 1, 2, fd) == 0)
434
428
    {
435
 
      IFDBG printf (" Function: write_gshort: Error while writing '%s'\n", why);
 
429
      IFDBG printf (" Function: write_gint16: Error while writing '%s'\n", why);
436
430
      gimp_quit ();
437
431
    }
438
432
}
441
435
 
442
436
 
443
437
static void
444
 
write_glong (FILE *fd, glong val, gchar *why)
 
438
write_gint32 (FILE *fd, gint32 val, gchar *why)
445
439
{
446
440
  unsigned char b[4];
447
441
 
452
446
 
453
447
  if (fwrite (&b, 1, 4, fd) == 0)
454
448
    {
455
 
      IFDBG printf (" Function: write_glong: Error while writing '%s'\n", why);
 
449
      IFDBG printf (" Function: write_gint32: Error while writing '%s'\n", why);
456
450
      gimp_quit ();
457
451
    }
458
452
}
459
453
 
460
454
 
461
 
static glong
462
 
pack_pb_line (guchar *start, glong length, glong stride,
463
 
               guchar *dest_ptr)
 
455
static gint32
 
456
pack_pb_line (guchar *start, gint32 length, gint32 stride,
 
457
               guchar *dest_ptr)
464
458
{
465
459
  gint i,j;
466
 
  glong remaining;
 
460
  gint32 remaining;
467
461
  remaining = length;
468
462
  length = 0;
469
463
  while (remaining > 0)
532
526
    case GIMP_INDEXED:
533
527
      return 2;                                         /* Indexed */
534
528
    default:
535
 
      g_message ("Error: Can't convert GIMP base imagetype to PSD mode");
 
529
      g_message (_("Error: Can't convert GIMP base imagetype to PSD mode"));
536
530
      IFDBG printf ("PSD Save: gimpBaseType value is %d, can't convert to PSD mode", gimpBaseType);
537
531
      gimp_quit ();
538
532
      return 3;                            /* Return RGB by default */
541
535
 
542
536
 
543
537
static gint
544
 
nChansLayer (gint gimpBaseType, gint hasAlpha)
 
538
nChansLayer (gint gimpBaseType, gint hasAlpha, gint hasMask)
545
539
{
546
540
  gint incAlpha = 0;
 
541
  gint incMask = 0;
547
542
 
548
543
  incAlpha = (hasAlpha == 0) ? 0 : 1;
 
544
  incMask = (hasMask == 0) ? 0 : 1;
549
545
 
550
546
  switch (gimpBaseType)
551
547
    {
552
548
    case GIMP_RGB:
553
 
      return 3 + incAlpha;               /* R,G,B & Alpha (if any) */
 
549
      return 3 + incAlpha + incMask;     /* R,G,B & Alpha & Mask (if any) */
554
550
    case GIMP_GRAY:
555
 
      return 1 + incAlpha;               /* G & Alpha (if any) */
 
551
      return 1 + incAlpha + incMask;     /* G & Alpha & Mask (if any) */
556
552
    case GIMP_INDEXED:
557
 
      return 1 + incAlpha;               /* I & Alpha (if any) */
 
553
      return 1 + incAlpha + incMask;     /* I & Alpha & Mask (if any) */
558
554
    default:
559
555
      return 0;                          /* Return 0 channels by default */
560
556
    }
567
563
  guchar *mapPSD;
568
564
  gint i;
569
565
 
570
 
  mapPSD = xmalloc (768);
 
566
  mapPSD = g_malloc (768);
571
567
 
572
568
  for (i = 0; i < 256; i++)
573
569
    {
589
585
save_header (FILE *fd, gint32 image_id)
590
586
{
591
587
  IFDBG printf (" Function: save_header\n");
592
 
  IFDBG printf ("      Rows: %d\n", PSDImageData.image_height);
593
 
  IFDBG printf ("      Columns: %d\n", PSDImageData.image_width);
594
 
  IFDBG printf ("      Base type: %d\n", PSDImageData.baseType);
595
 
  IFDBG printf ("      Number of channels: %d\n", PSDImageData.nChannels);
 
588
  IFDBG printf ("\tRows: %d\n", PSDImageData.image_height);
 
589
  IFDBG printf ("\tColumns: %d\n", PSDImageData.image_width);
 
590
  IFDBG printf ("\tBase type: %d\n", PSDImageData.baseType);
 
591
  IFDBG printf ("\tNumber of channels: %d\n", PSDImageData.nChannels);
596
592
 
597
593
  xfwrite (fd, "8BPS", 4, "signature");
598
 
  write_gshort (fd, 1, "version");
599
 
  write_glong (fd, 0, "reserved 1");       /* 6 for the 'reserved' field + 4 bytes for a long */
600
 
  write_gshort (fd, 0, "reserved 1");      /* and 2 bytes for a short */
601
 
  write_gshort (fd, (PSDImageData.nChannels +
602
 
                     nChansLayer (PSDImageData.baseType, 0)),
 
594
  write_gint16 (fd, 1, "version");
 
595
  write_gint32 (fd, 0, "reserved 1");      /* 6 for the 'reserved' field + 4 bytes for a long */
 
596
  write_gint16 (fd, 0, "reserved 1");      /* and 2 bytes for a short */
 
597
  write_gint16 (fd, (PSDImageData.nChannels +
 
598
                     nChansLayer (PSDImageData.baseType, 0, 0)),
603
599
                "channels");
604
 
  write_glong (fd, PSDImageData.image_height, "rows");
605
 
  write_glong (fd, PSDImageData.image_width, "columns");
606
 
  write_gshort (fd, 8, "depth");  /* Apparently GIMP only supports 8 bit deep
 
600
  write_gint32 (fd, PSDImageData.image_height, "rows");
 
601
  write_gint32 (fd, PSDImageData.image_width, "columns");
 
602
  write_gint16 (fd, 8, "depth");  /* Apparently GIMP only supports 8 bit deep
607
603
                                     PSD images.  */
608
 
  write_gshort (fd, gimpBaseTypeToPsdMode (PSDImageData.baseType), "mode");
 
604
  write_gint16 (fd, gimpBaseTypeToPsdMode (PSDImageData.baseType), "mode");
609
605
}
610
606
 
611
607
 
623
619
  switch (PSDImageData.baseType)
624
620
    {
625
621
    case GIMP_INDEXED:
626
 
      IFDBG printf ("      Image type: INDEXED\n");
 
622
      IFDBG printf ("\tImage type: INDEXED\n");
627
623
 
628
624
      cmap = gimp_image_get_colormap (image_id, &nColors);
629
 
      IFDBG printf ("      Length of colormap returned by gimp_image_get_colormap: %d\n", nColors);
 
625
      IFDBG printf ("\t\tLength of colormap returned by gimp_image_get_colormap: %d\n", nColors);
630
626
 
631
627
      if (nColors == 0)
632
628
        {
633
 
          IFDBG printf ("      The indexed image lacks a colormap\n");
634
 
          write_glong (fd, 0, "color data length");
 
629
          IFDBG printf ("\t\tThe indexed image lacks a colormap\n");
 
630
          write_gint32 (fd, 0, "color data length");
635
631
        }
636
632
      else if (nColors != 256)
637
633
        {
638
 
          IFDBG printf ("      The indexed image has %d!=256 colors\n", nColors);
639
 
          IFDBG printf ("      Padding with zeros up to 256\n");
640
 
          write_glong (fd, 768, "color data length");
 
634
          IFDBG printf ("\t\tThe indexed image has %d!=256 colors\n", nColors);
 
635
          IFDBG printf ("\t\tPadding with zeros up to 256\n");
 
636
          write_gint32 (fd, 768, "color data length");
641
637
            /* For this type, length is always 768 */
642
638
 
643
 
          cmap_modified = xmalloc (768);
 
639
          cmap_modified = g_malloc (768);
644
640
          for (i = 0; i < nColors * 3; i++)
645
641
            cmap_modified[i] = cmap[i];
646
642
 
654
650
        }
655
651
      else         /* nColors equals 256 */
656
652
        {
657
 
          write_glong (fd, 768, "color data length");   /* For this type, length is always 768 */
 
653
          write_gint32 (fd, 768, "color data length");   /* For this type, length is always 768 */
658
654
          reshuffle_cmap_write (cmap);
659
655
          xfwrite (fd, cmap, 768, "colormap");  /* Write readjusted colormap */
660
656
        }
661
657
      break;
662
658
 
663
659
    default:
664
 
      IFDBG printf ("      Image type: Not INDEXED\n");
665
 
      write_glong (fd, 0, "color data length");
 
660
      IFDBG printf ("\tImage type: Not INDEXED\n");
 
661
      write_gint32 (fd, 0, "color data length");
666
662
    }
667
663
}
668
664
 
677
673
  guint      nActiveLayer = 0;    /* Number of the active layer */
678
674
  gboolean   ActiveLayerPresent;  /* TRUE if there's an active layer */
679
675
 
680
 
  gint32     eof_pos;             /* Position for End of file */
681
 
  gint32     rsc_pos;             /* Position for Lengths of Resources section */
682
 
  gint32     name_sec;            /* Position for Lengths of Channel Names */
 
676
  glong      eof_pos;             /* Position for End of file */
 
677
  glong      rsc_pos;             /* Position for Lengths of Resources section */
 
678
  glong      name_sec;            /* Position for Lengths of Channel Names */
683
679
 
684
680
 
685
681
  /* Only relevant resources in GIMP are: 0x03EE, 0x03F0 & 0x0400 */
686
682
  /* For Adobe Photoshop version 4.0 these can also be considered:
687
 
     0x0408, 0x040A & 0x040B */
 
683
     0x0408, 0x040A & 0x040B (1006, 1008, 1024, 1032, 1034, and 1035) */
688
684
 
689
685
  IFDBG printf (" Function: save_resources\n");
690
686
 
692
688
  /* Get the image title from its filename */
693
689
 
694
690
  fileName = gimp_image_get_filename (image_id);
695
 
  IFDBG printf ("      Image title: %s\n", fileName);
 
691
  IFDBG printf ("\tImage title: %s\n", fileName);
696
692
 
697
693
  /* Get the active layer number id */
698
694
 
699
695
  idActLayer = gimp_image_get_active_layer (image_id);
700
 
  IFDBG printf ("      Current layer id: %d\n", idActLayer);
 
696
  IFDBG printf ("\tCurrent layer id: %d\n", idActLayer);
701
697
 
702
698
  ActiveLayerPresent = FALSE;
703
699
  for (i = 0; i < PSDImageData.nLayers; i++)
709
705
 
710
706
  if (ActiveLayerPresent)
711
707
    {
712
 
      IFDBG printf ("      Active layer number is: %d\n", nActiveLayer);
 
708
      IFDBG printf ("\t\tActive layer is number %d\n", nActiveLayer);
713
709
    }
714
710
  else
715
711
    {
716
 
      IFDBG printf ("      No active layer\n");
 
712
      IFDBG printf ("\t\tNo active layer\n");
717
713
    }
718
714
 
719
715
 
720
716
  /* Here's where actual writing starts */
721
717
 
722
718
  rsc_pos = ftell (fd);
723
 
  write_glong (fd, 0, "image resources length");
 
719
  write_gint32 (fd, 0, "image resources length");
724
720
 
725
721
 
726
722
  /* --------------- Write Channel names --------------- */
728
724
  if (PSDImageData.nChannels > 0)
729
725
    {
730
726
      xfwrite (fd, "8BIM", 4, "imageresources signature");
731
 
      write_gshort (fd, 0x03EE, "0x03EE Id");
 
727
      write_gint16 (fd, 0x03EE, "0x03EE Id"); /* 1006 */
732
728
      /* write_pascalstring (fd, Name, "Id name"); */
733
 
      write_gshort (fd, 0, "Id name"); /* Set to null string (two zeros) */
 
729
      write_gint16 (fd, 0, "Id name"); /* Set to null string (two zeros) */
734
730
 
735
731
    /* Mark current position in the file */
736
732
 
737
733
    name_sec = ftell (fd);
738
 
    write_glong (fd, 0, "0x03EE resource size");
 
734
    write_gint32 (fd, 0, "0x03EE resource size");
739
735
 
740
736
    /* Write all strings */
741
737
 
750
746
    eof_pos = ftell (fd);
751
747
 
752
748
    fseek (fd, name_sec, SEEK_SET);
753
 
    write_glong (fd, eof_pos - name_sec - sizeof (glong), "0x03EE resource size");
754
 
    IFDBG printf ("\n      Total length of 0x03EE resource: %d\n",
755
 
                  (int) (eof_pos - name_sec - sizeof (glong)));
 
749
    write_gint32 (fd, eof_pos - name_sec - sizeof (gint32), "0x03EE resource size");
 
750
    IFDBG printf ("\tTotal length of 0x03EE resource: %d\n",
 
751
                  (int) (eof_pos - name_sec - sizeof (gint32)));
756
752
 
757
753
    /* Return to EOF to continue writing */
758
754
 
760
756
 
761
757
    /* Pad if length is odd */
762
758
 
763
 
    if ((eof_pos - name_sec - sizeof (glong)) & 1)
 
759
    if ((eof_pos - name_sec - sizeof (gint32)) & 1)
764
760
      write_gchar (fd, 0, "pad byte");
765
761
  }
766
762
 
772
768
 
773
769
      /* Count the guides */
774
770
      while ((guide_id = gimp_image_find_next_guide(image_id, guide_id)))
775
 
        n_guides++;
 
771
        n_guides++;
776
772
 
777
773
      xfwrite (fd, "8BIM", 4, "imageresources signature");
778
 
      write_gshort (fd, 0x0408, "0x0408 Id (Guides)");
 
774
      write_gint16 (fd, 0x0408, "0x0408 Id (Guides)"); /* 1032 */
779
775
      /* write_pascalstring (fd, Name, "Id name"); */
780
 
      write_gshort (fd, 0, "Id name"); /* Set to null string (two zeros) */
781
 
      write_glong (fd, 16 + 5 * n_guides, "0x0408 resource size");
 
776
      write_gint16 (fd, 0, "Id name"); /* Set to null string (two zeros) */
 
777
      write_gint32 (fd, 16 + 5 * n_guides, "0x0408 resource size");
782
778
      /* Save grid and guide header */
783
 
      write_glong (fd,   1, "grid/guide header version");
784
 
      write_glong (fd, 576, "grid custom spacing horizontal");/* dpi*32/4??*/
785
 
      write_glong (fd, 576, "grid custom spacing vertical");  /* dpi*32/4??*/
786
 
      write_glong (fd, n_guides, "number of guides");
 
779
      write_gint32 (fd,   1, "grid/guide header version");
 
780
      write_gint32 (fd, 576, "grid custom spacing horizontal");/* dpi*32/4??*/
 
781
      write_gint32 (fd, 576, "grid custom spacing vertical");  /* dpi*32/4??*/
 
782
      write_gint32 (fd, n_guides, "number of guides");
787
783
 
788
784
      /* write the guides */
789
785
      while ((guide_id = gimp_image_find_next_guide(image_id, guide_id)))
790
786
        {
791
 
          gchar orientation;
792
 
          glong position;
793
 
          orientation = gimp_image_get_guide_orientation(image_id, guide_id);
794
 
          position    = 32 * gimp_image_get_guide_position(image_id, guide_id);
795
 
          orientation ^= 1; /* in the psd vert =0 , horiz = 1 */
796
 
          write_glong (fd, position, "Position of guide");
797
 
          write_gchar (fd, orientation, "Orientation of guide");
798
 
          n_guides--;
799
 
        }
 
787
          gchar orientation;
 
788
          gint32 position;
 
789
          orientation = gimp_image_get_guide_orientation(image_id, guide_id);
 
790
          position    = 32 * gimp_image_get_guide_position(image_id, guide_id);
 
791
          orientation ^= 1; /* in the psd vert =0 , horiz = 1 */
 
792
          write_gint32 (fd, position, "Position of guide");
 
793
          write_gchar (fd, orientation, "Orientation of guide");
 
794
          n_guides--;
 
795
        }
800
796
      if ((ftell(fd) & 1))
801
 
        write_gchar(fd, 0, "pad byte");
 
797
        write_gchar(fd, 0, "pad byte");
802
798
      if (n_guides != 0)
803
 
        g_warning("Screwed up guide resource:: wrong number of guides\n");
804
 
      IFDBG printf ("      Total length of 0x0400 resource: %d\n", (int) sizeof (gshort));
 
799
        g_warning("Screwed up guide resource:: wrong number of guides\n");
 
800
      IFDBG printf ("\tTotal length of 0x0400 resource: %d\n", (int) sizeof (gint16));
805
801
    }
806
802
 
807
803
  /* --------------- Write resolution data ------------------- */
809
805
    gdouble xres = 0, yres = 0;
810
806
    guint32 xres_fix, yres_fix;
811
807
    GimpUnit g_unit;
812
 
    gshort psd_unit;
 
808
    gint16 psd_unit;
813
809
 
814
810
    g_unit = gimp_image_get_unit (image_id);
815
811
    gimp_image_get_resolution (image_id, &xres, &yres);
818
814
      {
819
815
        gdouble factor = gimp_unit_get_factor (g_unit) / 10.0;
820
816
 
821
 
        xres /= factor;
 
817
        xres /= factor;
822
818
        yres /= factor;
823
819
 
824
 
        psd_unit = PSD_UNIT_CM;
 
820
        psd_unit = PSD_UNIT_CM;
825
821
      }
826
822
    else
827
823
      {
828
 
        psd_unit = PSD_UNIT_INCH;
 
824
        psd_unit = PSD_UNIT_INCH;
829
825
      }
830
826
 
831
827
    xres_fix = xres * 65536.0 + .5; /* Convert to 16.16 fixed point */
832
828
    yres_fix = yres * 65536.0 + .5; /* Convert to 16.16 fixed point */
833
829
 
834
830
    xfwrite (fd, "8BIM", 4, "imageresources signature (for resolution)");
835
 
    write_gshort(fd, 0x03ed, "0x03ed Id (resolution)");
836
 
    write_gshort (fd, 0, "Id name"); /* Set to null string (two zeros) */
837
 
    write_glong (fd, 16, "0x0400 resource size");
838
 
    write_glong (fd,  xres_fix, "hRes (16.16 fixed point)");
839
 
    write_gshort (fd, psd_unit, "hRes unit");
840
 
    write_gshort (fd, psd_unit, "width unit");
841
 
    write_glong (fd,  yres_fix, "vRes (16.16 fixed point)");
842
 
    write_gshort (fd, psd_unit, "vRes unit");
843
 
    write_gshort (fd, psd_unit, "height unit");
 
831
    write_gint16(fd, 0x03ed, "0x03ed Id (resolution)"); /* 1005 */
 
832
    write_gint16 (fd, 0, "Id name"); /* Set to null string (two zeros) */
 
833
    write_gint32 (fd, 16, "0x0400 resource size");
 
834
    write_gint32 (fd,  xres_fix, "hRes (16.16 fixed point)");
 
835
    write_gint16 (fd, psd_unit, "hRes unit");
 
836
    write_gint16 (fd, psd_unit, "width unit");
 
837
    write_gint32 (fd,  yres_fix, "vRes (16.16 fixed point)");
 
838
    write_gint16 (fd, psd_unit, "vRes unit");
 
839
    write_gint16 (fd, psd_unit, "height unit");
844
840
  }
845
841
  /* --------------- Write Active Layer Number --------------- */
846
842
 
847
843
  if (ActiveLayerPresent)
848
844
    {
849
845
      xfwrite (fd, "8BIM", 4, "imageresources signature");
850
 
      write_gshort (fd, 0x0400, "0x0400 Id");
 
846
      write_gint16 (fd, 0x0400, "0x0400 Id"); /* 1024 */
851
847
      /* write_pascalstring (fd, Name, "Id name"); */
852
 
      write_gshort (fd, 0, "Id name"); /* Set to null string (two zeros) */
853
 
      write_glong (fd, sizeof (gshort), "0x0400 resource size");
854
 
 
855
 
      /* Save title as gshort (length always even) */
856
 
 
857
 
      write_gshort (fd, nActiveLayer, "active layer");
858
 
 
859
 
      IFDBG printf ("      Total length of 0x0400 resource: %d\n", (int) sizeof (gshort));
 
848
      write_gint16 (fd, 0, "Id name"); /* Set to null string (two zeros) */
 
849
      write_gint32 (fd, sizeof (gint16), "0x0400 resource size");
 
850
 
 
851
      /* Save title as gint16 (length always even) */
 
852
 
 
853
      write_gint16 (fd, nActiveLayer, "active layer");
 
854
 
 
855
      IFDBG printf ("\tTotal length of 0x0400 resource: %d\n", (int) sizeof (gint16));
860
856
    }
861
857
 
862
858
 
865
861
  eof_pos = ftell (fd);
866
862
 
867
863
  fseek (fd, rsc_pos, SEEK_SET);
868
 
  write_glong (fd, eof_pos - rsc_pos - sizeof (glong), "image resources length");
869
 
  IFDBG printf ("      Resource section total length: %d\n",
870
 
                (int) (eof_pos - rsc_pos - sizeof (glong)));
 
864
  write_gint32 (fd, eof_pos - rsc_pos - sizeof (gint32), "image resources length");
 
865
  IFDBG printf ("\tResource section total length: %d\n",
 
866
                (int) (eof_pos - rsc_pos - sizeof (gint32)));
871
867
 
872
868
  /* Return to EOF to continue writing */
873
869
 
878
874
 
879
875
static int
880
876
get_compress_channel_data (guchar  *channel_data,
881
 
                           gint32   channel_cols,
882
 
                           gint32   channel_rows,
883
 
                           gint32   stride,
884
 
                           gshort  *LengthsTable,
885
 
                           guchar  *remdata)
 
877
                           gint32   channel_cols,
 
878
                           gint32   channel_rows,
 
879
                           gint32   stride,
 
880
                           gint16  *LengthsTable,
 
881
                           guchar  *remdata)
886
882
{
887
883
  gint    i;
888
884
  gint32  len;                 /* Length of compressed data */
900
896
 
901
897
      /* Create packed data for this row */
902
898
      LengthsTable[i] = pack_pb_line (start, channel_cols, stride,
903
 
                                       &remdata[len]);
 
899
                                       &remdata[len]);
904
900
      len += LengthsTable[i];
905
901
    }
906
902
 
907
 
  /*  return((len + channel_rows * sizeof (gshort)) + sizeof (gshort));*/
 
903
  /*  return((len + channel_rows * sizeof (gint16)) + sizeof (gint16));*/
908
904
  return len;
909
905
}
910
906
 
924
920
  gint     nChannelsLayer;              /* Number of channels of a layer */
925
921
  gint32   ChanSize;                    /* Data length for a channel */
926
922
  gchar   *layerName;                   /* Layer name */
927
 
 
928
 
  gint32   eof_pos;                     /* Position: End of file */
929
 
  gint32   ExtraDataPos;                /* Position: Extra data length */
930
 
  gint32   LayerMaskPos;                /* Position: Layer & Mask section length */
931
 
  gint32   LayerInfoPos;                /* Position: Layer info section length*/
932
 
  gint32 **ChannelLengthPos;            /* Position: Channel length */
933
 
 
934
 
 
935
 
  IFDBG printf (" Function: save_layer&mask\n");
 
923
  gint     mask;                        /* Layer mask */
 
924
 
 
925
  glong    eof_pos;                     /* Position: End of file */
 
926
  glong    ExtraDataPos;                /* Position: Extra data length */
 
927
  glong    LayerMaskPos;                /* Position: Layer & Mask section length */
 
928
  glong    LayerInfoPos;                /* Position: Layer info section length*/
 
929
  glong  **ChannelLengthPos;            /* Position: Channel length */
 
930
 
 
931
 
 
932
  IFDBG printf (" Function: save_layer_and_mask\n");
936
933
 
937
934
  /* Create first array dimension (layers, channels) */
938
935
 
939
 
  ChannelLengthPos = g_new (gint32 *, PSDImageData.nLayers);
 
936
  ChannelLengthPos = g_new (glong *, PSDImageData.nLayers);
940
937
 
941
938
  /* Layer and mask information section */
942
939
 
943
940
  LayerMaskPos = ftell (fd);
944
 
  write_glong (fd, 0, "layers & mask information length");
 
941
  write_gint32 (fd, 0, "layers & mask information length");
945
942
 
946
943
  /* Layer info section */
947
944
 
948
945
  LayerInfoPos = ftell (fd);
949
 
  write_glong (fd, 0, "layers info section length");
 
946
  write_gint32 (fd, 0, "layers info section length");
950
947
 
951
948
  /* Layer structure section */
952
949
 
953
 
  write_gshort (fd, PSDImageData.nLayers, "Layer structure count");
 
950
  write_gint16 (fd, PSDImageData.nLayers, "Layer structure count");
954
951
 
955
952
  /* Layer records section */
956
953
  /* GIMP layers must be written in reverse order */
957
954
 
958
955
  for (i = PSDImageData.nLayers - 1; i >= 0; i--)
959
956
    {
 
957
      gint hasMask = 0;
 
958
 
960
959
      gimp_drawable_offsets (PSDImageData.lLayers[i], &offset_x, &offset_y);
961
960
      layerWidth = gimp_drawable_width (PSDImageData.lLayers[i]);
962
961
      layerHeight = gimp_drawable_height (PSDImageData.lLayers[i]);
966
965
      PSDImageData.layersDim[i].width = layerWidth;
967
966
      PSDImageData.layersDim[i].height = layerHeight;
968
967
 
969
 
      IFDBG printf ("      Layer number: %d\n", i);
970
 
      IFDBG printf ("         Offset x: %d\n", PSDImageData.layersDim[i].left);
971
 
      IFDBG printf ("         Offset y: %d\n", PSDImageData.layersDim[i].top);
972
 
      IFDBG printf ("         Width: %d\n", PSDImageData.layersDim[i].width);
973
 
      IFDBG printf ("         Height: %d\n", PSDImageData.layersDim[i].height);
 
968
      IFDBG printf ("\tLayer number: %d\n", i);
 
969
      IFDBG printf ("\t\tX offset: %d\n", PSDImageData.layersDim[i].left);
 
970
      IFDBG printf ("\t\tY offset: %d\n", PSDImageData.layersDim[i].top);
 
971
      IFDBG printf ("\t\tWidth: %d\n", PSDImageData.layersDim[i].width);
 
972
      IFDBG printf ("\t\tHeight: %d\n", PSDImageData.layersDim[i].height);
974
973
 
975
 
      write_glong (fd, PSDImageData.layersDim[i].top, "Layer top");
976
 
      write_glong (fd, PSDImageData.layersDim[i].left, "Layer left");
977
 
      write_glong (fd, (PSDImageData.layersDim[i].height +
 
974
      write_gint32 (fd, PSDImageData.layersDim[i].top, "Layer top");
 
975
      write_gint32 (fd, PSDImageData.layersDim[i].left, "Layer left");
 
976
      write_gint32 (fd, (PSDImageData.layersDim[i].height +
978
977
                        PSDImageData.layersDim[i].top), "Layer bottom");
979
 
      write_glong (fd, (PSDImageData.layersDim[i].width +
 
978
      write_gint32 (fd, (PSDImageData.layersDim[i].width +
980
979
                        PSDImageData.layersDim[i].left), "Layer right");
981
980
 
 
981
      hasMask = (gimp_layer_get_mask(PSDImageData.lLayers[i]) == -1 ) ? 0 : 1;
982
982
      nChannelsLayer = nChansLayer (PSDImageData.baseType,
983
 
                                    gimp_drawable_has_alpha (PSDImageData.lLayers[i]));
984
 
 
985
 
 
986
 
      write_gshort (fd, nChannelsLayer, "Number channels in the layer");
987
 
      IFDBG printf ("         Number of channels: %d\n", nChannelsLayer);
 
983
                                    gimp_drawable_has_alpha (PSDImageData.lLayers[i]),
 
984
                                    hasMask);
 
985
 
 
986
 
 
987
      write_gint16 (fd, nChannelsLayer, "Number channels in the layer");
 
988
      IFDBG printf ("\t\tNumber of channels: %d\n", nChannelsLayer);
988
989
 
989
990
      /* Create second array dimension (layers, channels) */
990
991
 
991
 
      ChannelLengthPos[i] = g_new (gint32, nChannelsLayer);
 
992
      ChannelLengthPos[i] = g_new (glong, nChannelsLayer);
992
993
 
993
994
      /* Try with gimp_drawable_bpp() */
994
995
 
998
999
            idChannel = j - 1;
999
1000
          else
1000
1001
            idChannel = j;
 
1002
          if (hasMask && (j+1 == nChannelsLayer)) /* Last channel ... */
 
1003
            idChannel = -2; /* ... will be layer mask */
1001
1004
 
1002
 
          write_gshort (fd, idChannel, "Channel ID");
1003
 
          IFDBG printf ("           - Identifier: %d\n", idChannel);
 
1005
          write_gint16 (fd, idChannel, "Channel ID");
 
1006
          IFDBG printf ("\t\t\tChannel Identifier: %d\n", idChannel);
1004
1007
 
1005
1008
          /* Write the length assuming no compression.  In case there is,
1006
1009
             will modify it later when writing data.  */
1007
1010
 
1008
1011
          ChannelLengthPos[i][j] = ftell (fd);
1009
 
          ChanSize = sizeof (gshort) + (PSDImageData.layersDim[i].width *
 
1012
          ChanSize = sizeof (gint16) + (PSDImageData.layersDim[i].width *
1010
1013
                                        PSDImageData.layersDim[i].height);
1011
1014
 
1012
 
          write_glong (fd, ChanSize, "Channel Size");
1013
 
          IFDBG printf ("             Length: %d\n", ChanSize);
 
1015
          write_gint32 (fd, ChanSize, "Channel Size");
 
1016
          IFDBG printf ("\t\t\tLength: %d\n", ChanSize);
1014
1017
        }
1015
1018
 
1016
1019
      xfwrite (fd, "8BIM", 4, "blend mode signature");
1017
1020
 
1018
1021
      psd_lmode_layer (PSDImageData.lLayers[i], blendMode);
1019
 
      IFDBG printf ("         Blend mode: %s\n", blendMode);
 
1022
      IFDBG printf ("\t\tBlend mode: %s\n", blendMode);
1020
1023
      xfwrite (fd, blendMode, 4, "blend mode key");
1021
1024
 
1022
1025
      layerOpacity = (gimp_layer_get_opacity (PSDImageData.lLayers[i]) * 255.0) / 100.0;
1023
 
      IFDBG printf ("         Opacity: %u\n", layerOpacity);
 
1026
      IFDBG printf ("\t\tOpacity: %u\n", layerOpacity);
1024
1027
      write_gchar (fd, layerOpacity, "Opacity");
1025
1028
 
1026
1029
      /* Apparently this field is not used in GIMP */
1027
1030
      write_gchar (fd, 0, "Clipping");
1028
1031
 
1029
1032
      flags = 0;
1030
 
      if (gimp_layer_get_preserve_trans (PSDImageData.lLayers[i])) flags |= 1;
 
1033
      if (gimp_layer_get_lock_alpha (PSDImageData.lLayers[i])) flags |= 1;
1031
1034
      if (! gimp_drawable_get_visible (PSDImageData.lLayers[i])) flags |= 2;
1032
 
      IFDBG printf ("         Flags: %u\n", flags);
 
1035
      IFDBG printf ("\t\tFlags: %u\n", flags);
1033
1036
      write_gchar (fd, flags, "Flags");
1034
1037
 
1035
1038
      /* Padding byte to make the length even */
1036
1039
      write_gchar (fd, 0, "Filler");
1037
1040
 
1038
1041
      ExtraDataPos = ftell (fd); /* Position of Extra Data size */
1039
 
      write_glong (fd, 0, "Extra data size");
 
1042
      write_gint32 (fd, 0, "Extra data size");
1040
1043
 
1041
 
#ifdef SAVELAYERMASK
1042
1044
      mask = gimp_layer_get_mask (PSDImageData.lLayers[i]);
1043
1045
      if (mask  >= 0)
1044
1046
        {
1045
 
          write_glong  (fd, 14,                        "Layer mask size");
1046
 
          write_glong  (fd, 0,                         "Layer mask top");
1047
 
          write_glong  (fd, 0,                         "Layer mask left");
1048
 
          write_glong  (fd, gimp_drawable_height(mask),"Layer mask bottom");
1049
 
          write_glong  (fd, gimp_drawable_width(mask), "Layer mask right");
1050
 
          write_gchar  (fd, 0,                         "lmask default color");
1051
 
          flags = (1                                        | /* relative */
1052
 
                   (gimp_layer_mask_is_disabled(mask) << 1) | /* disabled?*/
1053
 
                   (0 << 2)                                   /* invert   */);
1054
 
          write_gchar  (fd, flags,                      "layer mask flags");
1055
 
          write_gshort (fd, 0,                          "Layer mask Padding");
1056
 
        }
 
1047
          IFDBG printf ("\t\tLayer mask size: %d\n", 20);
 
1048
          write_gint32  (fd, 20,                        "Layer mask size");
 
1049
          write_gint32  (fd, 0,                         "Layer mask top");
 
1050
          write_gint32  (fd, 0,                         "Layer mask left");
 
1051
          write_gint32  (fd, gimp_drawable_height(mask),"Layer mask bottom");
 
1052
          write_gint32  (fd, gimp_drawable_width(mask), "Layer mask right");
 
1053
          write_gchar  (fd, 0,                         "lmask default color");
 
1054
          flags = (1                                        | /* relative */
 
1055
                   (gimp_layer_get_apply_mask(PSDImageData.lLayers[i]) << 1) | /* disabled?*/
 
1056
                   (0 << 2)                                   /* invert   */);
 
1057
          write_gchar  (fd, flags,                      "layer mask flags");
 
1058
          write_gint16 (fd, 0,                          "Layer mask Padding");
 
1059
        }
1057
1060
      else
1058
 
#else
1059
 
      /* NOTE Writing empty Layer mask / adjustment layer data */
1060
 
      write_glong (fd, 0, "Layer mask size");
1061
 
      IFDBG printf ("\n         Layer mask size: %d\n", 0);
1062
 
#endif
 
1061
        {
 
1062
          /* NOTE Writing empty Layer mask / adjustment layer data */
 
1063
          write_gint32 (fd, 0, "Layer mask size");
 
1064
          IFDBG printf ("\t\tLayer mask size: %d\n", 0);
 
1065
        }
1063
1066
 
1064
1067
      /* NOTE Writing empty Layer blending ranges data */
1065
 
      write_glong (fd, 0, "Layer blending size");
1066
 
      IFDBG printf ("\n         Layer blending size: %d\n", 0);
 
1068
      write_gint32 (fd, 0, "Layer blending size");
 
1069
      IFDBG printf ("\t\tLayer blending size: %d\n", 0);
1067
1070
 
1068
1071
      layerName = gimp_drawable_get_name (PSDImageData.lLayers[i]);
1069
1072
      write_pascalstring (fd, layerName, 4, "layer name");
1070
 
      IFDBG printf ("\n         Layer name: %s\n", layerName);
 
1073
      IFDBG printf ("\t\tLayer name: %s\n", layerName);
1071
1074
 
1072
1075
      /* Write real length for: Extra data */
1073
1076
 
1074
1077
      eof_pos = ftell (fd);
1075
1078
 
1076
1079
      fseek (fd, ExtraDataPos, SEEK_SET);
1077
 
      write_glong (fd, eof_pos - ExtraDataPos - sizeof (glong), "Extra data size");
1078
 
      IFDBG printf ("      ExtraData size: %d\n",
1079
 
                    (int) (eof_pos - ExtraDataPos - sizeof (glong)));
 
1080
      write_gint32 (fd, eof_pos - ExtraDataPos - sizeof (gint32), "Extra data size");
 
1081
      IFDBG printf ("\t\tExtraData size: %d\n",
 
1082
                    (int) (eof_pos - ExtraDataPos - sizeof (gint32)));
1080
1083
 
1081
1084
      /* Return to EOF to continue writing */
1082
1085
 
1089
1092
 
1090
1093
  for (i = PSDImageData.nLayers - 1; i >= 0; i--)
1091
1094
    {
 
1095
      IFDBG printf ("\t\tWriting pixel data for layer slot %d\n", i);
1092
1096
      write_pixel_data(fd, PSDImageData.lLayers[i], ChannelLengthPos[i], 0);
1093
1097
    }
1094
1098
 
1097
1101
  /* Write actual size of Layer info section */
1098
1102
 
1099
1103
  fseek (fd, LayerInfoPos, SEEK_SET);
1100
 
  write_glong (fd, eof_pos - LayerInfoPos - sizeof (glong), "layers info section length");
1101
 
  IFDBG printf ("\n      Total layers info section length: %d\n",
1102
 
                (int) (eof_pos - LayerInfoPos - sizeof (glong)));
 
1104
  write_gint32 (fd, eof_pos - LayerInfoPos - sizeof (gint32), "layers info section length");
 
1105
  IFDBG printf ("\t\tTotal layers info section length: %d\n",
 
1106
                (int) (eof_pos - LayerInfoPos - sizeof (gint32)));
1103
1107
 
1104
1108
  /* Write actual size of Layer and mask information secton */
1105
1109
 
1106
1110
  fseek (fd, LayerMaskPos, SEEK_SET);
1107
 
  write_glong (fd, eof_pos - LayerMaskPos - sizeof (glong), "layers & mask information length");
1108
 
  IFDBG printf ("      Total layers & mask information length: %d\n",
1109
 
                (int) (eof_pos - LayerMaskPos - sizeof (glong)));
 
1111
  write_gint32 (fd, eof_pos - LayerMaskPos - sizeof (gint32), "layers & mask information length");
 
1112
  IFDBG printf ("\t\tTotal layers & mask information length: %d\n",
 
1113
                (int) (eof_pos - LayerMaskPos - sizeof (gint32)));
1110
1114
 
1111
1115
  /* Return to EOF to continue writing */
1112
1116
 
1116
1120
 
1117
1121
 
1118
1122
static void
1119
 
write_pixel_data (FILE *fd, gint32 drawableID, gint32 *ChanLenPosition,
1120
 
                  glong ltable_offset)
 
1123
write_pixel_data (FILE *fd, gint32 drawableID, glong *ChanLenPosition,
 
1124
                  gint32 ltable_offset)
1121
1125
{
1122
1126
  GimpPixelRgn region;      /* Image region */
1123
1127
  guchar *data;             /* Temporary copy of pixel data */
1125
1129
  gint32 tile_height = gimp_tile_height();
1126
1130
 
1127
1131
  GimpDrawable *drawable = gimp_drawable_get (drawableID);
 
1132
  gint32 maskID = gimp_layer_get_mask(drawableID);
1128
1133
 
1129
1134
  gint32 height = drawable->height;
1130
1135
  gint32 width  = drawable->width;
1132
1137
  gint32 colors = bytes;    /* fixed up down below */
1133
1138
  gint32 y;
1134
1139
 
1135
 
  glong   len;                  /* Length of compressed data */
1136
 
  gshort *LengthsTable;         /* Lengths of every compressed row */
 
1140
  gint32   len;                  /* Length of compressed data */
 
1141
  gint16 *LengthsTable;         /* Lengths of every compressed row */
1137
1142
  guchar *rledata;              /* Compressed data from a region */
1138
 
  gint32 length_table_pos;      /* position in file of the length table */
 
1143
  glong  length_table_pos;      /* position in file of the length table */
1139
1144
  int i, j;
1140
1145
 
 
1146
  IFDBG printf (" Function: write_pixel_data, drw %d, lto %d\n",
 
1147
                drawableID, ltable_offset);
 
1148
 
1141
1149
  if ( gimp_drawable_has_alpha  (drawableID) &&
1142
1150
      !gimp_drawable_is_indexed (drawableID))
1143
1151
    colors -= 1;
1144
1152
  gimp_tile_cache_ntiles (2* (drawable->width / gimp_tile_width () + 1));
1145
1153
 
1146
 
  LengthsTable = g_new (gshort, height);
1147
 
  rledata = g_new (gchar, (MIN(height, tile_height) * 
1148
 
                           (width + 10 + (width/100))));
1149
 
 
 
1154
  LengthsTable = g_new (gint16, height);
 
1155
  rledata = g_new (guchar, (MIN (height, tile_height) *
 
1156
                            (width + 10 + (width / 100))));
1150
1157
 
1151
1158
  data = g_new (guchar, MIN(height, tile_height) * width * bytes);
1152
1159
 
1153
1160
  gimp_pixel_rgn_init (&region, drawable, 0, 0,
1154
 
                       width, height, FALSE, FALSE);
 
1161
                       width, height, FALSE, FALSE);
1155
1162
 
1156
1163
  for (i = 0; i < bytes; i++)
1157
1164
    {
1159
1166
      len = 0;
1160
1167
      if (bytes != colors) /* Need to write alpha channel first */
1161
1168
        {
1162
 
          if (i == 0)
 
1169
          if (i == 0)
1163
1170
            {
1164
 
              if (ltable_offset > 0)
1165
 
                continue;
1166
 
              chan = bytes - 1;
1167
 
            }
1168
 
          else
1169
 
            chan = i - 1;
1170
 
        }
 
1171
              if (ltable_offset > 0)
 
1172
                continue;
 
1173
              chan = bytes - 1;
 
1174
            }
 
1175
          else
 
1176
            chan = i - 1;
 
1177
        }
1171
1178
      else
1172
 
        chan = i;
 
1179
        chan = i;
1173
1180
      if (ChanLenPosition)
1174
1181
        {
1175
 
          write_gshort (fd, 1, "Compression type (RLE)");
1176
 
          len += 2;
1177
 
        }
 
1182
          write_gint16 (fd, 1, "Compression type (RLE)");
 
1183
          len += 2;
 
1184
        }
1178
1185
 
1179
1186
      if (ltable_offset > 0)
1180
1187
        {
1181
 
          length_table_pos = ltable_offset + 2 * chan * height;
1182
 
        }
 
1188
          length_table_pos = ltable_offset + 2 * chan * height;
 
1189
        }
1183
1190
      else
1184
1191
        {
1185
 
          length_table_pos = ftell(fd);
 
1192
          length_table_pos = ftell(fd);
1186
1193
 
1187
 
          xfwrite (fd, LengthsTable, height * sizeof(gshort),
1188
 
                   "Dummy RLE length");
1189
 
          len += height * 2;
 
1194
          xfwrite (fd, LengthsTable, height * sizeof(gint16),
 
1195
                   "Dummy RLE length");
 
1196
          len += height * sizeof(gint16);
 
1197
          IF_DEEP_DBG printf ("\t\t\t\t. ltable, pos %ld len %d\n", length_table_pos, len);
1190
1198
        }
1191
1199
 
1192
1200
      for (y = 0; y < height; y += tile_height)
1193
 
        {
1194
 
          int tlen;
1195
 
          gimp_pixel_rgn_get_rect (&region, data, 0, y,
1196
 
                                   width, MIN(height - y, tile_height));
1197
 
          tlen = get_compress_channel_data (&data[chan],
1198
 
                                             width,
1199
 
                                             MIN(height - y, tile_height),
1200
 
                                             bytes,
1201
 
                                             &LengthsTable[y],
1202
 
                                             rledata);
1203
 
          len += tlen;
1204
 
          xfwrite (fd, rledata, tlen, "Compressed pixel data");
1205
 
        }
 
1201
        {
 
1202
          int tlen;
 
1203
            gimp_pixel_rgn_get_rect (&region, data, 0, y,
 
1204
                                   width, MIN(height - y, tile_height));
 
1205
          tlen = get_compress_channel_data (&data[chan],
 
1206
                                             width,
 
1207
                                             MIN(height - y, tile_height),
 
1208
                                             bytes,
 
1209
                                             &LengthsTable[y],
 
1210
                                             rledata);
 
1211
          len += tlen;
 
1212
          xfwrite (fd, rledata, tlen, "Compressed pixel data");
 
1213
          IF_DEEP_DBG printf ("\t\t\t\t. Writing compressed pixels, stream of %d\n", tlen);
 
1214
        }
1206
1215
 
1207
1216
      /* Write compressed lengths table */
1208
1217
      fseek (fd, length_table_pos, SEEK_SET);
1209
1218
      for (j = 0; j < height; j++) /* write real length table */
1210
 
        write_gshort (fd, LengthsTable[j], "RLE length");
1211
 
    
 
1219
        write_gint16 (fd, LengthsTable[j], "RLE length");
 
1220
 
1212
1221
      if (ChanLenPosition)    /* Update total compressed length */
1213
 
        {
1214
 
          fseek (fd, ChanLenPosition[i], SEEK_SET);
1215
 
          write_glong (fd, len, "channel data length");
1216
 
        }
 
1222
        {
 
1223
          fseek (fd, ChanLenPosition[i], SEEK_SET);
 
1224
          write_gint32 (fd, len, "channel data length");
 
1225
          IFDBG printf ("\t\tUpdating data len to %d\n", len);
 
1226
        }
1217
1227
      fseek (fd, 0, SEEK_END);
 
1228
      IF_DEEP_DBG printf ("\t\t\t\t. Cur pos %ld\n", ftell(fd));
1218
1229
    }
1219
1230
 
 
1231
  /* Write layer mask, as last channel, id -2 */
 
1232
  if (maskID != -1) {
 
1233
    GimpDrawable *mdrawable = gimp_drawable_get(maskID);
 
1234
    len = 0;
 
1235
 
 
1236
    gimp_pixel_rgn_init (&region, mdrawable, 0, 0,
 
1237
                         width, height, FALSE, FALSE);
 
1238
 
 
1239
    if (ChanLenPosition)
 
1240
      {
 
1241
        write_gint16 (fd, 1, "Compression type (RLE)");
 
1242
        len += 2;
 
1243
        IF_DEEP_DBG printf ("\t\t\t\t. ChanLenPos, len %d\n", len);
 
1244
      }
 
1245
 
 
1246
    if (ltable_offset > 0)
 
1247
      {
 
1248
        length_table_pos = ltable_offset + 2 * (bytes+1) * height;
 
1249
        IF_DEEP_DBG printf ("\t\t\t\t. ltable, pos %ld\n", length_table_pos);
 
1250
      }
 
1251
    else
 
1252
      {
 
1253
        length_table_pos = ftell(fd);
 
1254
 
 
1255
        xfwrite (fd, LengthsTable, height * sizeof(gint16),
 
1256
                 "Dummy RLE length");
 
1257
        len += height * sizeof(gint16);
 
1258
        IF_DEEP_DBG printf ("\t\t\t\t. ltable, pos %ld len %d\n", length_table_pos, len);
 
1259
      }
 
1260
 
 
1261
    for (y = 0; y < height; y += tile_height)
 
1262
      {
 
1263
        int tlen;
 
1264
        gimp_pixel_rgn_get_rect (&region, data, 0, y,
 
1265
                                 width, MIN(height - y, tile_height));
 
1266
        tlen = get_compress_channel_data (&data[0],
 
1267
                                          width,
 
1268
                                          MIN(height - y, tile_height),
 
1269
                                          1,
 
1270
                                          &LengthsTable[y],
 
1271
                                          rledata);
 
1272
        len += tlen;
 
1273
        xfwrite (fd, rledata, tlen, "Compressed mask data");
 
1274
        IF_DEEP_DBG printf ("\t\t\t\t. Writing compressed mask, stream of %d\n", tlen);
 
1275
      }
 
1276
 
 
1277
    /* Write compressed lengths table */
 
1278
    fseek (fd, length_table_pos, SEEK_SET); /*POS WHERE???*/
 
1279
    for (j = 0; j < height; j++) /* write real length table */
 
1280
      {
 
1281
        write_gint16 (fd, LengthsTable[j], "RLE length");
 
1282
        IF_DEEP_DBG printf ("\t\t\t\t. Updating RLE len %d\n", LengthsTable[j]);
 
1283
      }
 
1284
 
 
1285
    if (ChanLenPosition)    /* Update total compressed length */
 
1286
      {
 
1287
        fseek (fd, ChanLenPosition[bytes], SEEK_SET); /*+bytes OR SOMETHING*/
 
1288
        write_gint32 (fd, len, "channel data length");
 
1289
        IFDBG printf ("\t\tUpdating data len to %d, at %ld\n", len, ftell(fd));
 
1290
      }
 
1291
    fseek (fd, 0, SEEK_END);
 
1292
    IF_DEEP_DBG printf ("\t\t\t\t. Cur pos %ld\n", ftell(fd));
 
1293
 
 
1294
    gimp_drawable_detach (mdrawable);
 
1295
  }
 
1296
 
1220
1297
  gimp_drawable_detach (drawable);
1221
1298
 
1222
1299
  g_free (data);
1237
1314
  gint chan;
1238
1315
  gint32 bottom_layer;
1239
1316
 
1240
 
  IFDBG printf ("\n Function: save_data\n");
 
1317
  IFDBG printf (" Function: save_data\n");
1241
1318
 
1242
1319
  ChanCount = (PSDImageData.nChannels +
1243
 
               nChansLayer (PSDImageData.baseType, 0));
 
1320
               nChansLayer (PSDImageData.baseType, 0, 0));
1244
1321
 
1245
 
  i = PSDImageData.nLayers - 1;  /* Channel to be written */
1246
 
  IFDBG printf ("     Processing layer %d\n", i);
 
1322
  i = PSDImageData.nLayers - 1;  /* Layers to be written */
 
1323
  IFDBG printf ("\tProcessing %d layers\n", i);
1247
1324
 
1248
1325
  imageHeight = gimp_image_height (image_id);
1249
1326
 
1250
1327
  nChannel = 0;
1251
1328
 
1252
1329
 
1253
 
  write_gshort (fd, 1, "RLE compression");
1254
 
 
 
1330
  write_gint16 (fd, 1, "RLE compression");
 
1331
 
1255
1332
  /* All line lengths go before the rle pixel data */
1256
1333
 
1257
1334
  offset = ftell(fd); /* Offset in file of line lengths */
1258
1335
 
1259
1336
  for (i = 0; i < ChanCount; i++)
1260
1337
    for (j = 0; j < imageHeight; j++)
1261
 
      write_gshort (fd, 0, "junk line lengths");
1262
 
      
 
1338
      write_gint16 (fd, 0, "junk line lengths");
 
1339
 
1263
1340
  bottom_layer = PSDImageData.lLayers[PSDImageData.nLayers - 1];
1264
1341
 
1265
1342
  if (PSDImageData.nLayers != 1 ||
1269
1346
      gint32 flat_image;
1270
1347
      gint32 flat_drawable;
1271
1348
 
1272
 
      IFDBG printf ("\n     Creating flattened image\n");
 
1349
      IFDBG printf ("\t\tCreating flattened image\n");
1273
1350
      flat_image = gimp_image_duplicate (image_id);
1274
1351
      gimp_image_undo_disable (flat_image);
1275
1352
      flat_drawable = gimp_image_flatten (flat_image);
1276
1353
 
1277
 
      IFDBG printf ("\n     Writing compressed flattened image data\n");
1278
 
      write_pixel_data (fd, flat_drawable, NULL, offset);
 
1354
      /* gimp_image_flatten() may fail if there are no visible layers */
 
1355
      if (flat_drawable != -1)
 
1356
        {
 
1357
          IFDBG printf ("\t\tWriting compressed flattened image data\n");
 
1358
          write_pixel_data (fd, flat_drawable, NULL, offset);
 
1359
        }
1279
1360
 
1280
1361
      gimp_image_delete (flat_image);
1281
1362
    }
1282
1363
  else
1283
1364
    {
1284
 
      IFDBG printf ("\n     Writing compressed image data\n");
 
1365
      IFDBG printf ("\t\tWriting compressed image data\n");
1285
1366
      write_pixel_data (fd, PSDImageData.lLayers[PSDImageData.nLayers - 1],
1286
 
                        NULL, offset);
 
1367
                        NULL, offset);
1287
1368
    }
1288
1369
 
1289
 
  chan = nChansLayer (PSDImageData.baseType, 0);
 
1370
  chan = nChansLayer (PSDImageData.baseType, 0, 0);
1290
1371
 
1291
1372
  for (i = PSDImageData.nChannels - 1; i >= 0; i--)
1292
1373
    {
1293
 
      IFDBG printf ("\n     Writing compressed channel data for channel %d\n",
1294
 
                    i);
 
1374
      IFDBG printf ("\t\tWriting compressed channel data for channel %d\n",
 
1375
                    i);
1295
1376
      write_pixel_data (fd, PSDImageData.lChannels[i], NULL,
1296
 
                        offset + 2*imageHeight*chan);
 
1377
                        offset + 2*imageHeight*chan);
1297
1378
      chan++;
1298
1379
    }
1299
1380
}
1308
1389
  PSDImageData.compression = FALSE;
1309
1390
 
1310
1391
  PSDImageData.image_height = gimp_image_height (image_id);
1311
 
  IFDBG printf ("      Got number of rows: %d\n", PSDImageData.image_height);
 
1392
  IFDBG printf ("\tGot number of rows: %d\n", PSDImageData.image_height);
1312
1393
 
1313
1394
  PSDImageData.image_width = gimp_image_width (image_id);
1314
 
  IFDBG printf ("      Got number of cols: %d\n", PSDImageData.image_width);
 
1395
  IFDBG printf ("\tGot number of cols: %d\n", PSDImageData.image_width);
1315
1396
 
1316
1397
  PSDImageData.baseType = gimp_image_base_type (image_id);
1317
 
  IFDBG printf ("      Got base type: %d\n", PSDImageData.baseType);
 
1398
  IFDBG printf ("\tGot base type: %d\n", PSDImageData.baseType);
1318
1399
 
1319
1400
  /* PSD format does not support indexed layered images */
1320
1401
 
1321
1402
  if (PSDImageData.baseType == GIMP_INDEXED)
1322
1403
    {
1323
 
      IFDBG printf ("      Flattening indexed image\n");
 
1404
      IFDBG printf ("\tFlattening indexed image\n");
1324
1405
      gimp_image_flatten (image_id);
1325
1406
    }
1326
1407
 
1327
1408
  PSDImageData.lChannels = gimp_image_get_channels (image_id, &PSDImageData.nChannels);
1328
 
  IFDBG printf ("      Got number of channels: %d\n", PSDImageData.nChannels);
 
1409
  IFDBG printf ("\tGot number of channels: %d\n", PSDImageData.nChannels);
1329
1410
 
1330
1411
  PSDImageData.lLayers = gimp_image_get_layers (image_id, &PSDImageData.nLayers);
1331
 
  IFDBG printf ("      Got number of layers: %d\n", PSDImageData.nLayers);
 
1412
  IFDBG printf ("\tGot number of layers: %d\n", PSDImageData.nLayers);
1332
1413
 
1333
1414
  PSDImageData.layersDim = g_new (PSD_Layer_Dimension, PSDImageData.nLayers);
1334
1415
}
1343
1424
  gint32 *layers;
1344
1425
  gint    nlayers;
1345
1426
  gint    i;
1346
 
  char   *name_buf;
1347
1427
  GimpDrawable *drawable;
1348
1428
 
1349
1429
  IFDBG printf (" Function: save_image\n");
1363
1443
      drawable = gimp_drawable_get (layers[i]);
1364
1444
      if (drawable->width > 30000 || drawable->height > 30000)
1365
1445
        {
1366
 
          g_message (_("Unable to save '%s'.  The psd file format does not support images with layers that are more than 30000 pixels wide or tall."),
1367
 
                     gimp_filename_to_utf8 (filename));
1368
 
          g_free (layers);
1369
 
          return FALSE;
1370
 
        }
 
1446
          g_message (_("Unable to save '%s'.  The psd file format does not support images with layers that are more than 30000 pixels wide or tall."),
 
1447
                     gimp_filename_to_utf8 (filename));
 
1448
          g_free (layers);
 
1449
          return FALSE;
 
1450
        }
1371
1451
    }
1372
1452
  g_free (layers);
1373
1453
 
1374
1454
 
1375
 
  fd = fopen (filename, "wb");
 
1455
  fd = g_fopen (filename, "wb");
1376
1456
  if (fd == NULL)
1377
1457
    {
1378
1458
      g_message (_("Could not open '%s' for writing: %s"),
1380
1460
      return FALSE;
1381
1461
    }
1382
1462
 
1383
 
  name_buf = g_strdup_printf (_("Saving '%s'..."),
1384
 
                              gimp_filename_to_utf8 (filename));
1385
 
  gimp_progress_init (name_buf);
1386
 
  g_free (name_buf);
 
1463
  gimp_progress_init_printf (_("Saving '%s'"),
 
1464
                             gimp_filename_to_utf8 (filename));
1387
1465
 
1388
 
  IFDBG g_print ("      File \"%s\" has been opened\n",
 
1466
  IFDBG g_print ("\tFile '%s' has been opened\n",
1389
1467
                 gimp_filename_to_utf8 (filename));
1390
1468
 
1391
1469
  get_image_data (fd, image_id);
1396
1474
  /* PSD format does not support layers in indexed images */
1397
1475
 
1398
1476
  if (PSDImageData.baseType == GIMP_INDEXED)
1399
 
    write_glong (fd, 0, "layers info section length");
 
1477
    write_gint32 (fd, 0, "layers info section length");
1400
1478
  else
1401
1479
    save_layer_and_mask (fd, image_id);
1402
1480
 
1404
1482
 
1405
1483
  save_data (fd, image_id);
1406
1484
 
1407
 
  IFDBG printf ("\n\n");
 
1485
  IFDBG printf ("----- Closing PSD file, done -----\n\n");
1408
1486
 
1409
1487
  fclose (fd);
1410
1488
  return TRUE;